【问题标题】:Flutter dialog not showing [closed]颤振对话框未显示[关闭]
【发布时间】:2021-02-02 09:28:08
【问题描述】:

这是我的代码,这里我触发了函数showDialog,但没有显示任何对话框。

Widget appbar(BuildContext context) {
  return PreferredSize(
    preferredSize: Size.fromHeight(59.0), //AppBar Height
    child: AppBar(
      centerTitle: true, //make icon on center
      leading: Builder(
        builder: (context) => IconButton(
            icon: SizedBox(
                height: 32,
                width: 32,
                child: Image.asset('resources/icon/menu.png')),
            onPressed: () {
              if (_scaffoldBodyKey.currentState.isDrawerOpen == false) {
                _scaffoldBodyKey.currentState.openDrawer();
              } else {
                _scaffoldBodyKey.currentState.openEndDrawer();
              }
            }),
      ),
      title: SizedBox(
          height: 32, width: 32, child: Image.asset('resources/icon/icon.png')),
      actions: <Widget>[
        IconButton(
            icon: SizedBox(
                height: 25,
                width: 25,
                child: Image.asset('resources/icon/more.png')),
            onPressed: () {
              showDialog(
                context: context,
                builder: (ctx) => AlertDialog(
                  title: Text('Sfee'),
                  content: Text("dddsscsdcscs"),
                  actions: <Widget>[
                    Column(
                      children: [
                        FlatButton(
                          child: Text('Okay'),
                          onPressed: () {
                            Navigator.of(ctx).pop();
                          },
                          color: Theme.of(context).accentColor,
                          padding: EdgeInsets.all(6),
                        ),
                      ],
                    )
                  ],
                ),
              );
            }),
      ],
    ),
  );

【问题讨论】:

  • 任何错误控制台?
  • 确保提供的context 是构建方法上下文。
  • 试了主体也没有作用
  • 有弹窗launch.json
  • @JeffNaa 您介意关闭您的编辑器和模拟器并重新启动吗??

标签: flutter


【解决方案1】:

因此,您的context 并没有丢失,但context 在小部件树中没有MaterialLocalizations 小部件。意思是你需要MaterialApp,比如

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyPage(),
    );
  }
}

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar,//your appBar is not wrong
      body: Container() //your body
    );
  }

更多解释,check this

【讨论】:

  • @Dude 因为对话框,在 JeffNaa 的 appBar 编码中,到目前为止没有任何问题。也许您应该弄清楚导致此错误的原因。
【解决方案2】:

尝试这种格式的对话框

showDialog(
        context: context,
        builder: (_) => Dialog(
          child: Container(...),
        )
)

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 2022-11-11
    • 2018-11-13
    • 2020-08-14
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多