flutter dialog异常Another exception was thrown: No MaterialLocalizations found

flutter dialog异常Another exception was thrown: No MaterialLocalizations found

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

void main() {
  runApp(new RootLayout());
}

class RootLayout extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new RootLayoutM();
  }
}

class RootLayoutM extends State<RootLayout> {
  _showMyMaterialDialog(BuildContext context) {
    print("_showMyMaterialDialog");
    showDialog(
        context: context, 
        Builder: (context) { 
          return  new new AlertDialog ( 
            title: new new the Text ( "title" ), 
            Content: new new the Text ( "Content contents contents contents contents contents" ), 
            Actions: <the Widget> [
               new new FlatButton ( 
                onPressed: () { 
                  Navigator.of (context) .pop (); 
                }, 
                Child: new new the Text ( "confirmation" ), 
              ), 
              new new FlatButton (  
                onPressed: () {
                  Navigator.of (context) .pop (); 
                },
                child: new Text("取消"),
              ),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Center(
          child: new Text("show simple dialog",
              style: new TextStyle(color: Color(0xFF00FF00))),
        ),
        floatingActionButton: new FloatingActionButton(
            child: new Text("showDialog"),
            onPressed: () {
              _showMyMaterialDialog(context);
            }),
      ),
    );
    ;
  }
}

 Widget top here top of the context where the Widget belong StatefulWidget why it can not display dialog

Found here

 @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Center(
          child: new Text("show simple dialog",
              style: new TextStyle(color: Color(0xFF00FF00))),
        ),
        floatingActionButton: new FloatingActionButton(
            child: new Text("showDialog"),
            onPressed: () {
              _showMyMaterialDialog(context);
            }),
      ),
    );
    ;
  }

This FloatingActionButton outer layer of bread on it

class MyFloat the extends StatelessWidget { 
  _showMyMaterialDialog (BuildContext context) { 
    Print ( "_showMyMaterialDialog" ); 
    the showDialog ( 
        context: context, 
        Builder: (context) { 
          return  new new AlertDialog ( 
            title: new new the Text ( "title" ), 
            Content: new new the Text ( " content content content content content content content content content content content " ), 
            Actions: <the Widget> [
               new new FlatButton ( 
                onPressed: () { 
                  Navigator.of (context) .pop (); 
                },
                child: new Text("确认"),
              ),
              new FlatButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: new Text("取消"),
              ),
            ],
          );
        });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new FloatingActionButton(
        child: new Text("showDialog"),
        onPressed: () {
          _showMyMaterialDialog(context);
        });
  }

 

The complete code is as follows

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

void main() {
  runApp(new RootLayout());
}

class RootLayout extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new RootLayoutM();
  }
}

class RootLayoutM extends State<RootLayout> {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Center(
          child: new Text("show simple dialog",
              style: new TextStyle(color: Color(0xFF00FF00))),
        ),
        floatingActionButton: new MyFloat(),
      )
    );
  }
 
}

class MyFloat extends StatelessWidget{
  _showMyMaterialDialog(BuildContext context) {
    print("_showMyMaterialDialog");
    showDialog(
        context: context,
        builder: (context) {
          return new new AlertDialog ( 
            title: new new the Text ( "title" ), 
            Content: new new the Text ( "content content content content content content content content content content content" ), 
            Actions: <the Widget> [
               new new FlatButton ( 
                onPressed: () { 
                  Navigator.of (context) .pop (); 
                }, 
                Child: new new the Text ( "confirmation" ), 
              ), 
              new new FlatButton ( 
                onPressed: () { 
                  Navigator.of (context) .pop (); 
                }, 
                Child: new new Text("取消"),
              ),
            ],
          );
        });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new FloatingActionButton(
        child: new Text("showDialog"),
        onPressed: () {
          _showMyMaterialDialog(context);
        });
  }

}

 

Guess you like

Origin www.cnblogs.com/mingfeng002/p/11585760.html