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 AlertDialog(
            title: new Text("title"),
            content: new Text("內容內容內容內容內容內容內容內容內容內容內容"),
            actions: <Widget>[
              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) {
    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);
            }),
      ),
    );
    ;
  }
}

 這里頂層的context所在的Widget的頂層Widget屬於StatefulWidget為什么還不能顯示dialog呢

這里發現

 @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); }), ),
    );
    ;
  }

這個FloatingActionButton在外面包一層就可以了

class MyFloat extends StatelessWidget{
  _showMyMaterialDialog(BuildContext context) {
    print("_showMyMaterialDialog");
    showDialog(
        context: context,
        builder: (context) {
          return new AlertDialog(
            title: new Text("title"),
            content: new Text("內容內容內容內容內容內容內容內容內容內容內容"),
            actions: <Widget>[
              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);
        });
  }

 

完整代碼如下

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 AlertDialog(
            title: new Text("title"),
            content: new Text("內容內容內容內容內容內容內容內容內容內容內容"),
            actions: <Widget>[
              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);
        });
  }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM