我调用Navigator pushReplacement在我的flutter应用程序中显示一个新视图,并希望立即弹出一个简单的对话框,将该页面介绍给用户.(我希望用户能够在后台看到新视图)
如果我在窗口小部件的构建方法中调用showDialog,然后从构建方法返回窗口小部件(脚手架),我会收到错误,指出flutter已经在绘制窗口小部件.我希望我需要监听构建完成事件然后调用showDialog.
关于如何做到这一点的指导意见.
我想根据条件显示警告对话框.不基于按钮按下事件等用户交互.
如果在应用程序状态中设置了标志,则会显示数据警告对话框,否则不显示.
下面是我想要显示的示例警报对话框
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();
},
),
],
);
},
);
}
Run Code Online (Sandbox Code Playgroud)
我试图在主屏幕小部件的构建方法中调用该方法,但它给了我错误 -
The context used to push or pop routes from the Navigator must be that of a …Run Code Online (Sandbox Code Playgroud) 当存在特定错误状态接收且列表数据不为空时,我找不到在列内显示对话框警报的方法。警报对话框不涉及任何按钮单击,并且所有在线示例都包含 onTap 按钮。
\n我想在列表数据不为空并且设置了错误标志时显示警报。
\n下面是我的代码:
\n @override\n Widget build(BuildContext context) {\n return Scaffold(\n key: scaffoldKey,\n appBar: _myAppBar(),\n body: RefreshIndicator(\n key: _refreshIndicatorKey,\n onRefresh: handleRefresh,\n child: isListError && ordersListData.isEmpty\n ? _mainErrorWidget()\n : Column(\n children: <Widget>[\n Expanded(\n child: NotificationListener<ScrollNotification>(\n onNotification: (ScrollNotification scrollInfo) {\n if (hasMoreToLoad &&\n !isListLoading &&\n !isListError &&\n scrollInfo.metrics.pixels ==\n scrollInfo.metrics.maxScrollExtent) {\n handleLoadMore();\n }\n },\n child: ListView.builder(\n itemCount: ordersListData.length,\n itemBuilder: (BuildContext context, int index) {\n return Card(\n child: InkWell(\n onTap: () {\n Navigator.push(\n context,\n MaterialPageRoute(\n builder: (context) => OrderDetailsPage(\n …Run Code Online (Sandbox Code Playgroud) 我正在使用initState并出现导航器问题:
I / flutter(5726):构建Builder时引发了以下断言:I / flutter(5726):在构建过程中调用了setState()或markNeedsBuild()。
我的代码:
@override
void initState() {
super.initState();
print(globals.isLoggedIn);
if(globals.isLoggedIn) {
print("Already login");
Navigator.push(context, MaterialPageRoute(builder: (context)=> Dashboard()));
}
Run Code Online (Sandbox Code Playgroud)