AlertDialog
showDialog(
barrierDismissible, //点击遮罩背景是否关闭弹窗
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Alert弹窗'),
content: const Text('Alert弹窗内容'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();//关闭弹窗
},
child: const Text('按钮'),
)
],
);
},
);
获取点击按钮回调,使用 async-await
,关闭弹窗时在pop
方法中传入回调参数
void _showDialog() async {
var result = await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Alert弹窗'),
content: const Text('Alert弹窗内容'),
actions: [
TextButton(
onPressed: () {
//关闭弹窗,并传递回调参数
Navigator.pop(context,'确认');
},
child: const Text('按钮'),
)
],
);
},
);
print(result); //关闭弹窗时传的参数'确认'
}
SimpleDialog
Select弹窗 用SimpleDialog
生成select选项
List<String> prolist = ['手机', '笔记本', '汽车', '家具'];
showDialog(
barrierDismissible: true, //点击遮罩背景是否关闭弹窗
context: context,
builder: (context) {
return SimpleDialog(
children: prolist.map((pro) {
return SimpleDialogOption(
onPressed: () {
Navigator.pop(context);
},
child: Text(pro),
);
}).toList(),
);
},
);
ActionSheet 底部弹窗
showModalBottomSheet(
context: context,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
builder: (context) {
return Container(
// height: 300,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: prolist.map((pro) {
return ListTile(
title: Text(pro),
onTap: () {
Navigator.pop(context);
});
}).toList(),
),
);
},
);
Toast
使用fluttertoas插件
import 'package:fluttertoast/fluttertoast.dart';
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT, //Android平台显示时长
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,//IOS和web平台显示时长
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0,
webBgColor: "#f00",
webPosition: 'center',
);
标签:Toast,const,Text,context,return,Flutter,弹窗
From: https://www.cnblogs.com/angelwgh/p/17913663.html