No MaterialLocalizations found.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nimeghbia/article/details/83899654

flutter 显示一个dialog的时候显示出这个log

E/flutter ( 3466): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter ( 3466): No MaterialLocalizations found.
E/flutter ( 3466): MyHomePage widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
E/flutter ( 3466): Localizations are used to generate many different messages, labels,and abbreviations which are used by the material library. 
E/flutter ( 3466): To introduce a MaterialLocalizations, either use a  MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
E/flutter ( 3466): The specific widget that could not find a MaterialLocalizations ancestor was:
E/flutter ( 3466):   MyHomePage
E/flutter ( 3466): The ancestors of this widget were:
E/flutter ( 3466):   [root]

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  int _counter = 0;
  AnimationController animationController;
  Animation animation;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    animationController = new AnimationController(
        vsync: this, duration: Duration(milliseconds: 3000));
    animation = Tween(begin: 1.0, end: 2.0).animate(CurvedAnimation(
        parent: animationController, curve: Curves.fastOutSlowIn))
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          animationController.reverse();
        }
        if (status == AnimationStatus.dismissed) {
          animationController.forward();
        }
      })
      ..addListener(() {
        setState(() {});
      });
    animationController.repeat();
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    Future.delayed(Duration.zero, () => showMyDialog(context));
    return new MaterialApp(
//        localizationsDelegates: [
//          // ... app-specific localization delegate[s] here
//          GlobalMaterialLocalizations.delegate,
//          GlobalWidgetsLocalizations.delegate,
//        ],
//        supportedLocales: [
//          const Locale('zh', 'CH'),
//        ],

        home: new Scaffold(
      body: new Center(
          child: new Stack(
        children: <Widget>[
          Transform.translate(
            offset: Offset(animation.value, animation.value),
            child: Image.asset(
              "assets/cute1.jpg",
              width: 21.0,
              height: 21.0,
              fit: BoxFit.fill,
            ),
          ),
          Transform.translate(
            offset: Offset(animation.value * 2, animation.value * 2),
            child: Image.asset(
              "assets/add.png",
              width: 21.0,
              height: 21.0,
              fit: BoxFit.fill,
            ),
          ),
//            new RaisedButton(
//                child: new Text('提交'),
//                onPressed: () {
//                  showMyDialog(context);
//                }
//            )
        ],
      )),
      floatingActionButton: new FloatingActionButton(
        onPressed: (){showMyDialog(context);},
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.


        ));
    // return
  }

  void showMyDialog(BuildContext context) {
    showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(

            content: const Text(
              'Message',
            ),
            actions: <Widget>[
              FlatButton(
                child: const Text('OK'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
              ),
            ],


        );

//      return new SimpleDialog(
//        title: new Text('Test'),
//        children: <Widget>[
//          new RadioListTile(
//            title: new Text('Testing'), value: null, groupValue: null, onChanged: (value) {},
//          )
//        ],
//      );
       // return
      },
    );
  }
}

main函数直接运行的myHomepage,直接运行是没有问题的,但是只要显示一个dialog就会出错,最后外面有加了一层stateless,ok了

猜你喜欢

转载自blog.csdn.net/nimeghbia/article/details/83899654