我下面有代码
onPressed: () {
ShowAlert(
message: Text,
context: context,
icon: Icon(
Icons.check,
));}
我的ShowAlert
class ShowAlert<T> extends StatefulWidget{
.....
}
我的意思是,当用户onPressed
时,该应用将显示小警报。
现在,我希望警报在不按按钮的情况下自动显示。
我该怎么做?
答案 0 :(得分:1)
创建一个班级以显示您的dialog
。例如:将其放置在名为Dialog.dart的文件中:
class Dialog{
static showMyDialog(
BuildContext context,
) async {
await showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: Text("title"),
content: Text("label"),
actions: <Widget>[
RaisedButton(
onPressed: () => Navigator.pop(context),
child: Text(
"Action",
style: TextStyle(color: Colors.white),
)),
],
);
});
}
}
如果要在构建小部件后显示它:
@override
Widget build(BuildContext context) {
Dialogs.showMyDialog(context); //add this
return Scaffold(
...
}