Flutter:EasyLoading库找不到MediaQuery小部件错误 [英] Flutter: EasyLoading library No MediaQuery widget found error

查看:80
本文介绍了Flutter:EasyLoading库找不到MediaQuery小部件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EasyLoading库(我的代码:

  void main(){runApp(MyApp());configLoading();}MyApp类扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回FlutterEasyLoading(子代:GestureDetector(onTap:(){FocusScopeNode currentFocus = FocusScope.of(context);如果(!currentFocus.hasPrimaryFocus){currentFocus.unfocus();}},子:MaterialApp(主页:StartPage(),debugShowCheckedModeBanner:否,),));}} 

解决方案

您可以在
下复制粘贴运行完整代码摘自官方示例

完整代码

  import'dart:async';导入'package:flutter/material.dart';导入'package:flutter/cupertino.dart';导入'package:flutter_easyloading/flutter_easyloading.dart';void main(){runApp(MyApp());configLoading();}无效configLoading(){EasyLoading.instance..displayDuration = const持续时间(毫秒:2000)..indicatorType = EasyLoadingIndicatorType.fadingCircle..loadingStyle = EasyLoadingStyle.dark..indicatorSize = 45.0..radius = 10.0..progressColor =颜色.yellow..backgroundColor =颜色.绿色..indicatorColor =颜色.黄色..textColor =颜色.yellow..maskColor = Colors.blue.withOpacity(0.5)..userInteractions = true;}MyApp类扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回MaterialApp(标题:"Flutter EasyLoading",主题:ThemeData(primarySwatch:Colors.blue,),builder:(BuildContext上下文,Widget子项){返回FlutterEasyLoading(子代:GestureDetector(onTap:(){FocusScopeNode currentFocus = FocusScope.of(context);如果(!currentFocus.hasPrimaryFocus){currentFocus.unfocus();}},孩子:孩子,));},主页:StartPage(),);}}StartPage类扩展了StatefulWidget {StartPage({Key key}):超级(key:key);@override_StartPageState createState()=>_StartPageState();}类_StartPageState扩展了State< StartPage>{计时器_timer;双进度@override无效的initState(){super.initState();///为该帧的结尾安排一个回调WidgetsBinding.instance.addPostFrameCallback((_){EasyLoading.showSuccess('在initState中使用!');});}@override窗口小部件build(BuildContext context){返回脚手架(appBar:AppBar(标题:Text("test"),),身体:容器(宽度:MediaQuery.of(context).size.width,子级:SingleChildScrollView(子:列(crossAxisAlignment:CrossAxisAlignment.center,mainAxisAlignment:MainAxisAlignment.start,子代:< Widget> [加高按钮(onPressed:(){Navigator.pushReplacement(上下文,MaterialPageRoute(builder:(context)=> ExamplePage()));},子级:Text("click")),文本域(),柱子(子代:< Widget> [FlatButton(textColor:Colors.blue,子代:Text('dismiss'),onPressed:(){_timer?.cancel();EasyLoading.dismiss();},),FlatButton(textColor:Colors.blue,子代:文字('show'),onPressed:(){_timer?.cancel();EasyLoading.show(状态:'正在加载...');},),FlatButton(textColor:Colors.blue,子级:Text('showProgress'),onPressed:(){_progress = 0;_timer?.cancel();_timer = Timer.periodic(const持续时间(毫秒:100),(计时器计时器){EasyLoading.showProgress(_progress,状态:'$ {(_ progress * 100).toStringAsFixed(0)}%');_progress + = 0.03;如果(_progress> = 1){_timer?.cancel();EasyLoading.dismiss();}});},),FlatButton(textColor:Colors.blue,子级:Text('showSuccess'),onPressed:(){_timer?.cancel();EasyLoading.showSuccess('伟大的成功!');},),FlatButton(textColor:Colors.blue,子级:Text('showError'),onPressed:(){_timer?.cancel();EasyLoading.showError('失败失败');},),FlatButton(textColor:Colors.blue,子级:Text('showInfo'),onPressed:(){_timer?.cancel();EasyLoading.showInfo('有用的信息.');},),FlatButton(textColor:Colors.blue,子级:Text('showToast'),onPressed:(){_timer?.cancel();EasyLoading.showToast('Toast');},),],),填充(填充:EdgeInsets.only(top:20.0),子:列(子代:< Widget> [文字(样式"),填充(填充:EdgeInsets.only(top:10.0),子代:CupertinoSegmentedControl< EasyLoadingStyle>(selectedColor:Colors.blue,孩子们: {EasyLoadingStyle.dark:填充(填充:EdgeInsets.all(5.0),子级:Text("dark"),),EasyLoadingStyle.light:填充(填充:EdgeInsets.all(5.0),子级:Text("light"),),EasyLoadingStyle.custom:填充(填充:EdgeInsets.all(5.0),子级:Text("custom"),),},onValueChanged :(值){EasyLoading.instance.loadingStyle =值;},),),],),),填充(填充:EdgeInsets.only(top:20.0),子:列(子代:< Widget> [文字('MaskType'),填充(填充:EdgeInsets.only(top:10.0),子代:CupertinoSegmentedControl< EasyLoadingMaskType>(selectedColor:Colors.blue,孩子们: {EasyLoadingMaskType.none:填充(填充:EdgeInsets.all(5.0),子级:Text("none"),),EasyLoadingMaskType.clear:填充(填充:EdgeInsets.all(5.0),子级:Text("clear"),),EasyLoadingMaskType.black:填充(填充:EdgeInsets.all(5.0),子级:Text("black"),),EasyLoadingMaskType.custom:填充(填充:EdgeInsets.all(5.0),子级:Text("custom"),),},onValueChanged :(值){EasyLoading.instance.maskType =值;},),),],),),填充(填充:EdgeInsets.only(top:20.0),子:列(子代:< Widget> [Text('IndicatorType(total:23)'),填充(填充:EdgeInsets.only(top:10.0),孩子:CupertinoSegmentedControl< EasyLoadingIndicatorType>(selectedColor:Colors.blue,孩子们: {EasyLoadingIndicatorType.circle:填充(填充:EdgeInsets.all(5.0),子代:Text("circle"),),EasyLoadingIndicatorType.wave:填充(填充:EdgeInsets.all(5.0),子级:Text("wave"),),EasyLoadingIndicatorType.ring:填充(填充:EdgeInsets.all(5.0),子级:Text("ring"),),EasyLoadingIndicatorType.pulse:填充(填充:EdgeInsets.all(5.0),子级:Text("pulse"),),EasyLoadingIndicatorType.cubeGrid:填充(填充:EdgeInsets.all(5.0),子级:Text("cubeGrid"),),EasyLoadingIndicatorType.threeBounce:填充(填充:EdgeInsets.all(5.0),子级:Text("threeBounce"),),},onValueChanged :(值){EasyLoading.instance.indicatorType =值;},),),],),),],),),),);}}类ExamplePage扩展了StatefulWidget {@override_ExamplePageState createState()=>_ExamplePageState();}类_ExamplePageState扩展了State< ExamplePage>{@override窗口小部件build(BuildContext context){返回脚手架(appBar:AppBar(标题:文本(示例"),),身体:容器(子级:文本(示例页面"),));}} 

I use EasyLoading library(https://pub.dev/packages/flutter_easyloading). Everything works fine. But when I double click TextFormField this error occurs. I tried to make MaterialApp as root widget and put EasyLoading as child but this time EasyLoading not working. My code:

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

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {    
        return FlutterEasyLoading(
            child: GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus) {
              currentFocus.unfocus();
            }
          },
          child: MaterialApp(
            home: StartPage(),
            debugShowCheckedModeBanner: false,
          ),
        ));
      }
    }

解决方案

You can copy paste run full code below
From official example https://github.com/huangjianke/flutter_easyloading#how-to-use
You can put FlutterEasyLoading in builder of MaterialApp
code snippet

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter EasyLoading',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      builder: (BuildContext context, Widget child) {
        return FlutterEasyLoading(
            child: GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus) {
              currentFocus.unfocus();
            }
          },
          child: child,
        ));
      },
      home: StartPage(),
    );
  }
}

working demo

full code

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

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

void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = true;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter EasyLoading',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      builder: (BuildContext context, Widget child) {
        return FlutterEasyLoading(
            child: GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus) {
              currentFocus.unfocus();
            }
          },
          child: child,
        ));
      },
      home: StartPage(),
    );
  }
}

class StartPage extends StatefulWidget {
  StartPage({Key key}) : super(key: key);

  @override
  _StartPageState createState() => _StartPageState();
}

class _StartPageState extends State<StartPage> {
  Timer _timer;
  double _progress;

  @override
  void initState() {
    super.initState();

    /// Schedule a callback for the end of this frame
    WidgetsBinding.instance.addPostFrameCallback((_) {
      EasyLoading.showSuccess('Use in initState!');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test"),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              RaisedButton(
                  onPressed: () {
                    Navigator.pushReplacement(context,
                        MaterialPageRoute(builder: (context) => ExamplePage()));
                  },
                  child: Text("click")),
              TextField(),
              Column(
                children: <Widget>[
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('dismiss'),
                    onPressed: () {
                      _timer?.cancel();
                      EasyLoading.dismiss();
                    },
                  ),
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('show'),
                    onPressed: () {
                      _timer?.cancel();
                      EasyLoading.show(status: 'loading...');
                    },
                  ),
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('showProgress'),
                    onPressed: () {
                      _progress = 0;
                      _timer?.cancel();
                      _timer = Timer.periodic(const Duration(milliseconds: 100),
                          (Timer timer) {
                        EasyLoading.showProgress(_progress,
                            status: '${(_progress * 100).toStringAsFixed(0)}%');
                        _progress += 0.03;

                        if (_progress >= 1) {
                          _timer?.cancel();
                          EasyLoading.dismiss();
                        }
                      });
                    },
                  ),
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('showSuccess'),
                    onPressed: () {
                      _timer?.cancel();
                      EasyLoading.showSuccess('Great Success!');
                    },
                  ),
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('showError'),
                    onPressed: () {
                      _timer?.cancel();
                      EasyLoading.showError('Failed with Error');
                    },
                  ),
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('showInfo'),
                    onPressed: () {
                      _timer?.cancel();
                      EasyLoading.showInfo('Useful Information.');
                    },
                  ),
                  FlatButton(
                    textColor: Colors.blue,
                    child: Text('showToast'),
                    onPressed: () {
                      _timer?.cancel();
                      EasyLoading.showToast('Toast');
                    },
                  ),
                ],
              ),
              Padding(
                padding: EdgeInsets.only(top: 20.0),
                child: Column(
                  children: <Widget>[
                    Text('Style'),
                    Padding(
                      padding: EdgeInsets.only(top: 10.0),
                      child: CupertinoSegmentedControl<EasyLoadingStyle>(
                        selectedColor: Colors.blue,
                        children: {
                          EasyLoadingStyle.dark: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("dark"),
                          ),
                          EasyLoadingStyle.light: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("light"),
                          ),
                          EasyLoadingStyle.custom: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("custom"),
                          ),
                        },
                        onValueChanged: (value) {
                          EasyLoading.instance.loadingStyle = value;
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 20.0),
                child: Column(
                  children: <Widget>[
                    Text('MaskType'),
                    Padding(
                      padding: EdgeInsets.only(top: 10.0),
                      child: CupertinoSegmentedControl<EasyLoadingMaskType>(
                        selectedColor: Colors.blue,
                        children: {
                          EasyLoadingMaskType.none: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("none"),
                          ),
                          EasyLoadingMaskType.clear: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("clear"),
                          ),
                          EasyLoadingMaskType.black: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("black"),
                          ),
                          EasyLoadingMaskType.custom: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("custom"),
                          ),
                        },
                        onValueChanged: (value) {
                          EasyLoading.instance.maskType = value;
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 20.0),
                child: Column(
                  children: <Widget>[
                    Text('IndicatorType(total: 23)'),
                    Padding(
                      padding: EdgeInsets.only(top: 10.0),
                      child:
                          CupertinoSegmentedControl<EasyLoadingIndicatorType>(
                        selectedColor: Colors.blue,
                        children: {
                          EasyLoadingIndicatorType.circle: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("circle"),
                          ),
                          EasyLoadingIndicatorType.wave: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("wave"),
                          ),
                          EasyLoadingIndicatorType.ring: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("ring"),
                          ),
                          EasyLoadingIndicatorType.pulse: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("pulse"),
                          ),
                          EasyLoadingIndicatorType.cubeGrid: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("cubeGrid"),
                          ),
                          EasyLoadingIndicatorType.threeBounce: Padding(
                            padding: EdgeInsets.all(5.0),
                            child: Text("threeBounce"),
                          ),
                        },
                        onValueChanged: (value) {
                          EasyLoading.instance.indicatorType = value;
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class ExamplePage extends StatefulWidget {
  @override
  _ExamplePageState createState() => _ExamplePageState();
}

class _ExamplePageState extends State<ExamplePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("example"),
        ),
        body: Container(
          child: Text("example page"),
        ));
  }
}

这篇关于Flutter:EasyLoading库找不到MediaQuery小部件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆