码迷,mamicode.com
首页 > 其他好文 > 详细

No MaterialLocalizations found (Flutter)

时间:2019-03-04 12:50:25      阅读:1741      评论:0      收藏:0      [点我收藏+]

标签:als   page   welcome   appbar   body   oid   assign   let   caff   

在显示SimpleDialog时候程序报错 No MaterialLocalizations found 没有找到 MaterialLocalizations

 

搜索找到原因 runApp 需要先调用 StatelessWidget 返回一个 MaterialApp实例.

void main() => runApp(MainPage());

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: Welcome to Flutter,
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

 也就是说runApp 直接接受一个 StatefulWidget就会出现这个错误

 

完整代码

import package:flutter/material.dart;

void main() => runApp(MainPage());

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: Welcome to Flutter,
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePage createState() {
    return _HomePage(title: 简单对话框组件);
  }
}

enum Department { treasury, state }

class _HomePage extends State<HomePage> {
  _HomePage({Key key, @required this.title});

  final String title;

  ThemeData themeData = new ThemeData.light();
  Future<void> _askedToLead() async {
    switch (await showDialog<Department>(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: const Text(Select assignment),
            children: <Widget>[
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, Department.treasury);
                },
                child: const Text(Treasury department),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, Department.state);
                },
                child: const Text(State department),
              ),
            ],
          );
        })) {
      case Department.treasury:
        // Let‘s go.
        // ...
        break;
      case Department.state:
        // ...
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: this.title,
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(this.title),
        ),
        body: null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.format_list_numbered),
          onPressed: _askedToLead,
        ),
      ),
      theme: themeData,
    );
  }
}

 

No MaterialLocalizations found (Flutter)

标签:als   page   welcome   appbar   body   oid   assign   let   caff   

原文地址:https://www.cnblogs.com/z45281625/p/10469978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!