如何在不单击按钮的情况下在颤振中显示警报对话框

Gau*_*ain 4 flutter

当存在特定错误状态接收且列表数据不为空时,我找不到在列内显示对话框警报的方法。警报对话框不涉及任何按钮单击,并且所有在线示例都包含 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                                        baseurl: widget.baseurl,\n                                        username: widget.username,\n                                        password: widget.password,\n                                        id: ordersListData[index]["id"],\n                                      ),\n                                    ),\n                                  );\n                                },\n                                child: Row(\n                                    mainAxisAlignment: MainAxisAlignment.start,\n                                    mainAxisSize: MainAxisSize.max,\n                                    crossAxisAlignment:\n                                        CrossAxisAlignment.center,\n                                    children: <Widget>[\n                                      Expanded(\n                                        child: Padding(\n                                          padding: const EdgeInsets.all(10.0),\n                                          child: Column(\n                                              mainAxisAlignment:\n                                                  MainAxisAlignment.start,\n                                              mainAxisSize: MainAxisSize.min,\n                                              crossAxisAlignment:\n                                                  CrossAxisAlignment.start,\n                                              children: <Widget>[\n                                                _orderDate(\n                                                    ordersListData[index]),\n                                                _orderIdAndBillingName(\n                                                    ordersListData[index]),\n                                                _orderStatus(\n                                                    ordersListData[index]),\n                                                _orderTotal(\n                                                    ordersListData[index])\n                                              ]),\n                                        ),\n                                      )\n                                    ]),\n                              ),\n                            );\n                          }),\n                    ),\n                  ),\n                  if (isListLoading)\n                    Container(\n                      height: 60,\n                      child: Center(\n                          child: SpinKitPulse(\n                        color: Colors.purple,\n                        size: 50,\n                      )),\n                    ),\n                    _showErrorAlert(),\n                ],\n              ),\n      ),\n    );\n  }\n\nvoid _showErrorAlert() {\n    if (isListError && ordersListData.isNotEmpty) {\n      showDialog<void>(\n          context: context,\n          barrierDismissible: false,\n          builder: (BuildContext context) {\n            return AlertDialog(\n              title: Text("Retry"),\n              titlePadding: EdgeInsets.fromLTRB(15, 20, 15, 0),\n              content: Column(\n                children: <Widget>[\n                  listError ?? Text(listError),\n                  Text(\n                    "Do you want to retry?",\n                    style: Theme.of(context).textTheme.body1,\n                  ),\n                ],\n              ),\n              contentPadding: EdgeInsets.fromLTRB(15, 10, 15, 0),\n              actions: <Widget>[\n                FlatButton(\n                  child: Text("No"),\n                  onPressed: () {\n                    Navigator.of(context).pop();\n                  },\n                ),\n                FlatButton(\n                  child: Text("Yes"),\n                  onPressed: () {\n                    Navigator.of(context).pop();\n                    fetchOrdersList();\n                  },\n                )\n              ],\n            );\n          });\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

我收到以下异常:

\n
I/flutter (15007): \xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nI/flutter (15007): The following assertion was thrown building OrdersListPage(dirty, dependencies:\nI/flutter (15007): [_LocalizationsScope-[GlobalKey#a6289], _InheritedTheme], state: _OrdersListPageState#ed7e3):\nI/flutter (15007): setState() or markNeedsBuild() called during build.\nI/flutter (15007): This Overlay widget cannot be marked as needing to build because the framework is already in the\nI/flutter (15007): process of building widgets.  A widget can be marked as needing to be built during the build phase       \nI/flutter (15007): only if one of its ancestors is currently building. This exception is allowed because the framework      \nI/flutter (15007): builds parent widgets before children, which means a dirty descendant will always be built.\nI/flutter (15007): Otherwise, the framework might not visit this widget during this build phase.\nI/flutter (15007): The widget on which setState() or markNeedsBuild() was called was:\nI/flutter (15007):   Overlay-[LabeledGlobalKey<OverlayState>#d7fb9]\nI/flutter (15007): The widget which was currently being built when the offending call was made was:\nI/flutter (15007):   OrdersListPage\nI/flutter (15007):\nI/flutter (15007): The relevant error-causing widget was:\nI/flutter (15007):   OrdersListPage file:///C:/Users/itsga/Desktop/flutter/woocommerceadmin/lib/main.dart:121:13\nI/flutter (15007):\nI/flutter (15007): When the exception was thrown, this was the stack:\nI/flutter (15007): #0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:3896:11)  \nI/flutter (15007): #1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3911:6)\nI/flutter (15007): #2      State.setState (package:flutter/src/widgets/framework.dart:1168:14)\nI/flutter (15007): #3      OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:344:5)\nI/flutter (15007): #4      OverlayRoute.install (package:flutter/src/widgets/routes.dart:44:24)\nI/flutter (15007): #5      TransitionRoute.install (package:flutter/src/widgets/routes.dart:181:11)\nI/flutter (15007): #6      ModalRoute.install (package:flutter/src/widgets/routes.dart:959:11)\nI/flutter (15007): #7      NavigatorState.push (package:flutter/src/widgets/navigator.dart:1791:11)\nI/flutter (15007): #8      showGeneralDialog (package:flutter/src/widgets/routes.dart:1634:65)\nI/flutter (15007): #9      showDialog (package:flutter/src/material/dialog.dart:711:10)\nI/flutter (15007): #10     _OrdersListPageState._showErrorAlert (package:woocommerceadmin/src/orders/widgets/OrdersListPage.dart:408:7)\nI/flutter (15007): #11     _OrdersListPageState.build (package:woocommerceadmin/src/orders/widgets/OrdersListPage.dart:143:21)\nI/flutter (15007): #12     StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)\nI/flutter (15007): #13     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)\nI/flutter (15007): #14     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)\nI/flutter (15007): #15     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2432:33)\nI/flutter (15007): #16     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:773:20)\nI/flutter (15007): #17     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)I/flutter (15007): #18     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)       \nI/flutter (15007): #19     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)\nI/flutter (15007): #20     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)\nI/flutter (15007): #24     _invoke (dart:ui/hooks.dart:259:10)\nI/flutter (15007): #25     _drawFrame (dart:ui/hooks.dart:217:3)\nI/flutter (15007): (elided 3 frames from package dart:async)\nI/flutter (15007): \nI/flutter (15007): \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90     \n
Run Code Online (Sandbox Code Playgroud)\n

Cop*_*oad 8

问题是showDialog不返回任何内容Widget,因此您不能将其用作child.

你能做的是,

@override
void initState() {
  super.initState();

  SchedulerBinding.instance.addPostFrameCallback((_) { 
    _showErrorAlert();
  });
}
Run Code Online (Sandbox Code Playgroud)