首页 > 解决方案 > 屏幕加载后如何显示对话框(没有 onTap) Flutter

问题描述

我试图在页面加载时显示一个对话框。我有在点击按钮时调用的对话框我尝试使用相同的技术,但我得到不同的错误。

这是我的代码的开始部分:

class SelectAccount extends StatelessWidget {
  final globalKey = GlobalKey<ScaffoldState>();
  @override
  Widget build(BuildContext context) {
    return ScopedModel(
        model: userModel,
        child: ScopedModelDescendant<UserModel>(builder:
            (BuildContext inContext, Widget inChild, UserModel inModel) {
          return Scaffold(
              key: globalKey,
              extendBodyBehindAppBar: true,
              appBar: AppBar(),
              body: Stack(children: [
                Background(),
                Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                          top: 15 * SizeConfig.heightMultiplier),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Text(
                            'Please select',
                            style: Theme.of(context).textTheme.headline3,
                          ),
                        ],
                      ),
                      //color: Colors.red,
                    ),
                    Expanded(
                        flex: 2,
                        child: Column(...

在调用它之前我在哪里定义这个函数?我需要一个 initState() 吗?

showAlertDialog(BuildContext context) {
  // set up the buttons
  Widget cancelButton = FlatButton(
    child: Text("Cancel"),
    onPressed: () {},
  );
  Widget continueButton = FlatButton(
    child: Text("Continue"),
    onPressed: () {},
  );

  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("AlertDialog"),
    content:
        Text("Information here"),
    actions: [
      cancelButton,
      continueButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

像这样使用它:

                                    TextSpan(
                                        text: ' Sign in',
                                        style: TextStyle(
                                          color: buttonColor,
                                          fontSize: 14.0,
                                        ),
                                        recognizer: TapGestureRecognizer()
                                          ..onTap = () {
                                            Navigator.of(context)
                                                .pushReplacementNamed(
                                                    '/signin');
                                          })
                                  ]),
                            ),
                          ],
                        )),
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 70),
                    )
                  ],
                ),
                showAlertDialog(context)
              ]));
        }));
  }
}

showAlertDialog(BuildContext context) {
  // set up the buttons
  Widget cancelButton = FlatButton(
    child: Text("Cancel"),
    onPressed: () {},
  );
  Widget continueButton = FlatButton(
    child: Text("Continue"),
    onPressed: () {},
  );

  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("AlertDialog"),
    content:
        Text("Would you like to continue learning how to use Flutter alerts?"),
    actions: [
      cancelButton,
      continueButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

...引发以下错误:

I/flutter ( 4658): RESPONSE []
E/flutter ( 4658): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2416 pos 18: '!navigator._debugLocked': is not true.
E/flutter ( 4658): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter ( 4658): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter ( 4658): #2      _RouteEntry.handlePush.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2416:18)
E/flutter ( 4658): #3      TickerFuture.whenCompleteOrCancel.thunk (package:flutter/src/scheduler/ticker.dart:399:15)
E/flutter ( 4658): #4      _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 4658): #5      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 4658): #6      _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter ( 4658): #7      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter ( 4658): #8      Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter ( 4658): #9      Future._completeWithValue (dart:async/future_impl.dart:529:5)
E/flutter ( 4658): #10     Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
E/flutter ( 4658): #11     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 4658): #12     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 4658): #13     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 4658): #14     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 4658): #15     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 4658): #16     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 4658):
I/flutter ( 4658): Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3524 pos 12: '!_debugLocked': is not true.
I/flutter ( 4658): Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3524 pos 12: '!_debugLocked': is not true.

我希望这是有道理的

标签: flutter

解决方案


将页面转换为有状态的小部件并覆盖该initState功能,然后在那里显示对话框。


推荐阅读