在 flutter 中自动加载应用程序主屏幕上的显示警报对话框
Show alert dialog on app main screen load automatically in flutter
我想根据条件显示警报对话框。不基于用户交互,例如按钮按下事件。
如果在应用程序状态数据警报对话框中设置了标志,否则不会显示。
下面是我要显示的示例警报对话框
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();
},
),
],
);
},
);
}
我试图在主屏幕小部件的构建方法中调用该方法,但它给了我错误 -
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 方法?
您必须将内容包装在另一个 Widget
(最好是无状态的)中。
示例:
更改自:
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Trial',
home: Scaffold(
appBar: AppBar(title: Text('List scroll')),
body: Container(
child: Text("Hello world"),
)));
}
}
到这个:
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Trial',
home: Scaffold(
appBar: AppBar(title: Text('List scroll')), body: new MyHome()));
}
}
class MyHome extends StatelessWidget { // Wrapper Widget
@override
Widget build(BuildContext context) {
Future.delayed(Duration.zero, () => showAlert(context));
return Container(
child: Text("Hello world"),
);
}
void showAlert(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("hi"),
));
}
}
注意:请参阅 以在 Future.delayed(Duration.zero,..)
中包装 show alert
我会把它放在 State
(StatefulWidget
的 initState
中。
将它放在 Stateless
小部件的 build
方法中很诱人,但这会多次触发您的警报。
在下面的示例中,当设备未连接到 Wifi 时显示警报,如果未连接则显示 [重试] 按钮。
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."),
)
);
}
}
我使用 Flutter 社区开发的包解决了它。这里 https://pub.dev/packages/after_layout
将此添加到您的 pubspec.yaml
after_layout: ^1.0.7+2
然后试试下面的例子
import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
class DialogDemo extends StatefulWidget {
@override
_DialogDemoState createState() => _DialogDemoState();
}
class _DialogDemoState extends State<DialogDemo>
with AfterLayoutMixin<DialogDemo> {
@override
void initState() {
super.initState();
}
@override
void afterFirstLayout(BuildContext context) {
_neverSatisfied();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
decoration: BoxDecoration(color: Colors.red),
),
);
}
Future<void> _neverSatisfied() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
只需覆盖 initState
并在 Future
或 Timer
:
中调用您的 _showDialog
方法
@override
void initState() {
super.initState();
// Use either of them.
Future(_showDialog);
Timer.run(_showDialog); // Requires import: 'dart:async'
}
这就是我如何以简单的方式实现的:
-
在主屏幕(或任何所需的小部件)的构建方法上方:
Future checkFirstRun(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isFirstRun = prefs.getBool('isFirstRun') ?? true;
if (isFirstRun) {
// Whatever you want to do, E.g. Navigator.push()
prefs.setBool('isFirstRun', false);
} else {
return null;
}
}
然后在您的小部件的 initState 上:
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => checkFirstRun(context));
}
这确保了构建小部件后函数是 运行。
如果您使用的是 bloc,则按照@mirkancal 在此答案中的建议使用 BlocListener
:
我想根据条件显示警报对话框。不基于用户交互,例如按钮按下事件。
如果在应用程序状态数据警报对话框中设置了标志,否则不会显示。
下面是我要显示的示例警报对话框
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();
},
),
],
);
},
);
}
我试图在主屏幕小部件的构建方法中调用该方法,但它给了我错误 -
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 方法?
您必须将内容包装在另一个 Widget
(最好是无状态的)中。
示例:
更改自:
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Trial',
home: Scaffold(
appBar: AppBar(title: Text('List scroll')),
body: Container(
child: Text("Hello world"),
)));
}
}
到这个:
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Trial',
home: Scaffold(
appBar: AppBar(title: Text('List scroll')), body: new MyHome()));
}
}
class MyHome extends StatelessWidget { // Wrapper Widget
@override
Widget build(BuildContext context) {
Future.delayed(Duration.zero, () => showAlert(context));
return Container(
child: Text("Hello world"),
);
}
void showAlert(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("hi"),
));
}
}
注意:请参阅 Future.delayed(Duration.zero,..)
我会把它放在 State
(StatefulWidget
的 initState
中。
将它放在 Stateless
小部件的 build
方法中很诱人,但这会多次触发您的警报。
在下面的示例中,当设备未连接到 Wifi 时显示警报,如果未连接则显示 [重试] 按钮。
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."),
)
);
}
}
我使用 Flutter 社区开发的包解决了它。这里 https://pub.dev/packages/after_layout
将此添加到您的 pubspec.yaml
after_layout: ^1.0.7+2
然后试试下面的例子
import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
class DialogDemo extends StatefulWidget {
@override
_DialogDemoState createState() => _DialogDemoState();
}
class _DialogDemoState extends State<DialogDemo>
with AfterLayoutMixin<DialogDemo> {
@override
void initState() {
super.initState();
}
@override
void afterFirstLayout(BuildContext context) {
_neverSatisfied();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
decoration: BoxDecoration(color: Colors.red),
),
);
}
Future<void> _neverSatisfied() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
只需覆盖 initState
并在 Future
或 Timer
:
_showDialog
方法
@override
void initState() {
super.initState();
// Use either of them.
Future(_showDialog);
Timer.run(_showDialog); // Requires import: 'dart:async'
}
这就是我如何以简单的方式实现的:
在主屏幕(或任何所需的小部件)的构建方法上方:
Future checkFirstRun(BuildContext context) async { SharedPreferences prefs = await SharedPreferences.getInstance(); bool isFirstRun = prefs.getBool('isFirstRun') ?? true; if (isFirstRun) { // Whatever you want to do, E.g. Navigator.push() prefs.setBool('isFirstRun', false); } else { return null; } }
然后在您的小部件的 initState 上:
@override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) => checkFirstRun(context)); }
这确保了构建小部件后函数是 运行。
如果您使用的是 bloc,则按照@mirkancal 在此答案中的建议使用 BlocListener
: