在应用程序主屏幕加载时自动显示警报对话框

编程入门 行业动态 更新时间:2024-10-28 00:18:19
本文介绍了在应用程序主屏幕加载时自动显示警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想根据条件显示警报对话框.不基于用户交互,例如按钮按下事件.

I want to show alert dialog based on a condition. Not based on user interaction such as button press event.

如果在应用状态数据警报对话框中设置了标志,否则不会显示.

If a flag is set in app state data alert dialog is shown otherwise its not.

下面是我想显示的示例警报对话框

Below is the sample alert dialog which I want to show

void _showDialog() { // flutter defined function showDialog( context: context, builder: (BuildContext context) { // return object of type Dialog return AlertDialog( title: new Text("Alert Dialog title"), content: new Text("Alert Dialog body"), actions: <Widget>[ // usually buttons at the bottom of the dialog new FlatButton( child: new Text("Close"), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); }

我试图在主屏幕小部件的构建方法中调用该方法,但它给了我错误 -

I tried to call that method inside main screen widget's build method but it gives me error -

The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget. E/flutter ( 3667): #0 Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:1179:9) E/flutter ( 3667): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:1186:6) E/flutter ( 3667): #2 showDialog (package:flutter/src/material/dialog.dart:642:20)

问题是我不知道应该从哪里调用 _showDialog 方法?

Problem is I don't know from where I should call that _showDialog method?

推荐答案

我会把它放在 State(StatefulWidget).

将它放在 Stateless 小部件的 build 方法中很诱人,但这会多次触发您的警报.

Placing it in the build method of a Stateless widget is tempting, but that will trigger your alert multiple times.

在下面的这个例子中,当设备没有连接到 Wifi 时它会显示一个警报,如果没有连接到,则显示一个 [再试一次] 按钮.

In this example below, it displays an alert when the device is not connected to the Wifi, showing a [try again] button if it's not.

import 'package:flutter/material.dart'; import 'package:connectivity/connectivity.dart'; void main() => runApp(MaterialApp(title: "Wifi Check", home: MyPage())); class MyPage extends StatefulWidget { @override _MyPageState createState() => _MyPageState(); } class _MyPageState extends State<MyPage> { bool _tryAgain = false; @override void initState() { super.initState(); _checkWifi(); } _checkWifi() async { // the method below returns a Future var connectivityResult = await (new Connectivity().checkConnectivity()); bool connectedToWifi = (connectivityResult == ConnectivityResult.wifi); if (!connectedToWifi) { _showAlert(context); } if (_tryAgain != !connectedToWifi) { setState(() => _tryAgain = !connectedToWifi); } } @override Widget build(BuildContext context) { var body = Container( alignment: Alignment.center, child: _tryAgain ? RaisedButton( child: Text("Try again"), onPressed: () { _checkWifi(); }) : Text("This device is connected to Wifi"), ); return Scaffold( appBar: AppBar(title: Text("Wifi check")), body: body ); } void _showAlert(BuildContext context) { showDialog( context: context, builder: (context) => AlertDialog( title: Text("Wifi"), content: Text("Wifi not detected. Please activate it."), ) ); } }

更多推荐

在应用程序主屏幕加载时自动显示警报对话框

本文发布于:2023-11-27 16:49:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   对话框   应用程序   加载   屏幕

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!