颤振对话框未显示

时间:2021-02-02 09:28:08

标签: flutter

这是我的代码,我在这里触发了函数 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),
                        ),
                      ],
                    )
                  ],
                ),
              );
            }),
      ],
    ),
  );

2 个答案:

答案 0 :(得分:0)

试试这种对话框格式

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

答案 1 :(得分:0)

因此,您的 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