我想通过按钮显示一个自定义对话框。但我不希望用户在触摸外部区域时关闭对话框。
我知道barrierDismissible: false,
可以工作,但只能在showDialog小部件中工作。在这种情况下,我需要这样做,但在对话框小部件中。
这就是我所拥有的:
//RC
class ShowDialogGameOver2RC extends StatefulWidget {
ShowDialogGameOver2RC({required this.score});
late int score;
@override
State<ShowDialogGameOver2RC> createState() => _ShowDialogGameOver2RC();
}
// class dialog GameOver
class _ShowDialogGameOver2RC extends State<ShowDialogGameOver2RC> {
@override
Widget build(BuildContext context) {
return Dialog(
barrierDismissible: false //it doesn't work
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
elevation: 2,
backgroundColor: Colors.transparent,
child: _buildChild(context),
);
}
使用堆栈并将第一个小部件放置为GestureDetector
class _ShowDialogGameOver2RCState extends State<ShowDialogGameOver2RC> {
bool showDialog = true;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
GestureDetector(
onTap: () {
setState(() {
showDialog = !showDialog;
});
},
),
if (showDialog)
Dialog(
// barrierDismissible: false //it doesn't work
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
elevation: 2,
backgroundColor: Colors.transparent,
child: Container(
child: Column(
children: [Text("tada")],
),
),
),
],
),
);
}
}