【发布时间】:2025-12-11 23:20:04
【问题描述】:
我有一个如下所示的弹出窗口。我试图在弹出窗口的右上角放置一个关闭按钮,但那里似乎有一些我无法摆脱的额外空间。
我也面临一个错误,当我按下 OKAY 按钮时,即使我为它提供了 Navigator.pop(context),弹出窗口也不会被关闭。
这是我的代码:
class Popup {
final String title;
final String message;
final String rightButton;
final VoidCallback onTapRightButton;
final String leftButton;
final VoidCallback onTapLeftButton;
...
show(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return _PopupCall(
title: title,
message: message,
leftButton: leftButton ?? 'null',
rightButton: rightButton,
onTapLeftButton: onTapLeftButton,
onTapRightButton: onTapRightButton);
},
);
}
...
@override
Widget build(BuildContext context) {
return ScaleTransition(
scale: scaleAnimation,
child: AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(24),
),
),
title: Wrap(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Align(
alignment: Alignment.topRight,
child: IconButton(
icon: Icon(
Icons.close,
color: Colors.red,
size: 25,
),
onPressed: () {
Navigator.pop(context);
},
),
),
Center(
child: Text(widget.title,
style: TextStyle(
fontFamily: 'TTNorms',
fontWeight: FontWeight.bold,
wordSpacing: 0,
letterSpacing: 0,
fontSize: 25,
color: Colors.yellow,
)),
),
const SizedBox(height: 15.0),
Text(widget.message,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'TTNorms',
fontWeight: FontWeight.w400,
wordSpacing: 0,
letterSpacing: 0,
fontSize: 15,
color: Colors.yellow,
)),
const SizedBox(height: 16.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Visibility(
visible: widget.leftButton != 'null',
child: Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Center(
child: GestureDetector(
onTap: () {
if (widget.leftButton != 'null') {
widget
.onTapLeftButton(); //function to be performed onTap
}
Navigator.pop(context);
},
child: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
border: Border.all(
color: Colors.yellow, width: 2.0),
borderRadius: BorderRadius.all(
Radius.circular(25),
),
color: Colors.blue),
child: Center(
child: Text('Okay',
style: TextStyle(
fontFamily: 'TTNorms',
fontWeight: FontWeight.bold,
wordSpacing: 0,
letterSpacing: 0,
fontSize: 15,
color: Colors.yellow,
)),
),
),
),
),
),
),
Padding(
padding: EdgeInsets.only(
bottom: 10.0,
left: 10,
),
child: Center(
child: GestureDetector(
onTap: () {
widget
.onTapRightButton(); //function to be performed onTap
Navigator.pop(context);
},
child: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(15),
),
gradient: kGradientBackground,
),
child: Center(
child: Text(widget.rightButton.toUpperCase(),
style: TextStyle(
fontFamily: 'TTNorms',
fontWeight: FontWeight.bold,
wordSpacing: 0,
letterSpacing: 0,
fontSize: 15,
color: Colors.blue,
)),
),
),
),
),
),
],
),
],
),
],
),
),
);
}
这是弹出窗口的样子:
这就是我调用弹出窗口的方式:
Popup(
title: 'Oops!',
message:
"Looks like there has been a mistake please check later!",
rightButton: 'OK',
onTapLeftButton: () {},
onTapRightButton: () {},
).show(context);
【问题讨论】:
标签: flutter user-interface dart