flutter记录问题:No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provi

背景:

        只是写了一个按钮,想弹一个dialog,然后报了如下错误:

flutter记录问题:No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provi_第1张图片

在警告中可以看到这句话:

 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.

要引入MaterialLocalizations,可以在应用程序的根目录中使用MaterialApp来自动包含它们,或者添加一个带有MaterialLocalizations委托的本地化小部件

检查当前布局:

首先是main入口:

 

然后是SplashPage:

flutter记录问题:No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provi_第2张图片 

界面可以正常加载,但是当点击按钮之后,出现了上面的错误。 

利用Fluter Dev Tools 分析当前视图,再看错误提示中提到,需要根目录中使用MaterialApp包裹组件,恍然大悟,随改动进行尝试。

flutter记录问题:No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provi_第3张图片

 改动后的结构:

首先,在入口处新建一个MyApp继承StatelessWidget,使用MaterialApp进行包裹,代码如下:

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(), //主题
      // home: const MyHomePage(title: 'Flutter Demo Home Page',initValue: 1000,),
      home: SplashPage(),
    );
  }
}

再修改SplashPage的代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        alignment: Alignment.center,
        fit: StackFit.expand, //未定位widget占满Stack整个空间
        children: [
          Image.asset("assets/launch/splash_bg.png"),
          _isShowLogin ? getLoginView() : getProgressView(),
        ],
      ),
    );

重新运行构建,使用FlutterDevTools查看视图树:

flutter记录问题:No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provi_第4张图片

点击按钮,dialog正常展示!

 Tips: flutter dev tools如何打开:

以androidstudio 为例,然后会在浏览器中打开一个页面。

flutter记录问题:No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provi_第5张图片

参考:

 Flutter开发工具使用

你可能感兴趣的:(Fluter编译,flutter)