flutter
[flutter] 위젯에 함수 전달하기 - 무한반복 벗어나기
슈크림 붕어빵
2023. 7. 22. 21:44
class DeleteAlert extends StatefulWidget {
final VoidCallback function;
const DeleteAlert({
Key? key,
required this.function,
}) : super(key: key);
@override
State<DeleteAlert> createState() => _DeleteAlertScreenState();
}
위처럼 VoidCallback함수로 지정한다.
GestureDetector(
onTap: () {
widget.function();
finish(context);
},
child: Container(
decoration: BoxDecoration(
color: mallArrowColor,
borderRadius: BorderRadius.circular(22),
),
child: Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 5.0, bottom: 5.0),
child: Text(
"삭제",
style: TextStyle(
color: mallMainColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
),
),
)
사용할 때는 위처럼 widget.function()으로 사용한다. ()를 쓰면 호출하는 부분이 빠져 무한 반복이 일어나게 된다.
? GestureDetector(
onTap: () {
showInDialog(context,
backgroundColor: Colors.white,
contentPadding: EdgeInsets.zero,
dialogAnimation:
DialogAnimation.SLIDE_TOP_BOTTOM,
builder: (_) => DeleteAlert(
message: "정말 삭제하시겠습니까?",
button: "확인",
additionalWidget: Text(""),
function: () => sendDeleteComment(
context, comment.id),
));
},
child: Text(
"삭제하기",
style: TextStyle(
color: mallSubColor,
fontSize: 15,
),
),
)
위젯을 호출하며 함수를 보내줄 때는 위와 같이 사용한다.