在没有onPressed的情况下自动运行StatefulWidget

时间:2020-08-18 07:20:36

标签: flutter

我下面有代码

        onPressed: () {
            ShowAlert(
                message: Text,
                context: context,
                icon: Icon(
                  Icons.check,
                ));}

我的ShowAlert

class ShowAlert<T> extends StatefulWidget{
     .....
}

我的意思是,当用户onPressed时,该应用将显示小警报。 现在,我希望警报在不按按钮的情况下自动显示。

我该怎么做?

1 个答案:

答案 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(
   ...
  }