Flutter => showDialog / AlertDialog => No MaterialLocalizations found

前端 未结 3 896
情歌与酒
情歌与酒 2021-02-20 11:47

just new to Flutter but very impressed. I want to show a Dialog if a PushNotification arrives via Firebase \"onMessage\".

But every Time I get a Exception \"No MaterialL

相关标签:
3条回答
  • 2021-02-20 12:16

    Flutter 1.0, Dart 2.x

    This solution works on both StatelessWidget widget and StatefulWidget widget.

    On the top, in your declaration you can create a static navKey:

    class MyApp extends StatefulWidget {
      final String title; // sample var you want to pass to your widget
      static final navKey = new GlobalKey<NavigatorState>();
      const MyApp({Key navKey, this.title}) : super(key: navKey);
      @override
      State<StatefulWidget> createState() => _MyAppState();
    }
    

    On the layout part you should use the key:

    return MaterialApp(
            navigatorKey:MyApp.navKey,
            title: widget.title,
            ...
    

    So, when you need the current context for your dialog or other widget, in the state part you can do :

    @override
      void initState() {
      final context = MyApp.navKey.currentState.overlay.context;
      showMyCustomDialog(context);
      ...
      super.initState();
    }
    
    0 讨论(0)
  • 2021-02-20 12:23

    In Order to Fix the error, You need to Call Your Main class as a home parameter of MaterialAppas Like Below.

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Welcome to Flutter',
          debugShowCheckedModeBanner: false,
          home: Main(),
        );
      }
    }
    

    & update your Build Method in Main Class as:

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Welcome to Flutter'),
          ),
          body: Column(children: <Widget>[
            Center(
              child: Text('Hello World'),
            ),
            RaisedButton(
              onPressed: () {
                print("pushed?");
                _showPushDialog(context);
              },
              child: Text("press me"),
            )
          ]),
        );
      }
    
    0 讨论(0)
  • 2021-02-20 12:39

    check your MaterialApp() widget, if you set localizationsDelegates ,may be you well get this problem. it works when i delete those code.

                  localizationsDelegates: [
                      DefaultCupertinoLocalizations.delegate,
                  ],
    
    

    my codes.

    MaterialApp(
                  navigatorKey: navigatorKey,
                  title: 'test',
                  theme: ThemeData(
                      platform: TargetPlatform.iOS,
                      backgroundColor: Color(0xfff1f1f1),
                      accentColor: AppStyle.colorPrimary,
                      primaryColor: AppStyle.colorPrimary,
                      buttonColor: AppStyle.colorPrimary,
                      iconTheme: IconThemeData(color: Colors.white),
                      textTheme: TextTheme(
                          title: TextStyle(color: Colors.white),
                      ),
                      primaryTextTheme: TextTheme(title: TextStyle(color: Colors.white)),
                      primaryIconTheme: const IconThemeData.fallback().copyWith(
                          color: Colors.white,
                      ),
                      appBarTheme: AppBarTheme().copyWith(brightness: Brightness.dark),
                  ),
                  home: LoginPage(),
                  debugShowCheckedModeBanner: false,
                  onGenerateRoute: AppRouter.router.generator,
              )
    
    
    0 讨论(0)
提交回复
热议问题