【错误记录】Flutter 使用 MediaQuery 适配全面屏报错 ( No MediaQuery widget ancestor found. )
生活随笔
收集整理的這篇文章主要介紹了
【错误记录】Flutter 使用 MediaQuery 适配全面屏报错 ( No MediaQuery widget ancestor found. )
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 一、報錯信息
- 二、解決方案
一、報錯信息
需要使用 MediaQuery 獲取當前的 Padding ;
import 'package:flutter/material.dart';/// 使用 MediaQuery 進行全面屏適配 void main() {runApp(MyApp()); }class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {/// 獲取當前的 padding 信息 , 報錯位置 ; final EdgeInsets edgeInsets = MediaQuery.of(context).padding;return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: Container(decoration: BoxDecoration(color: Colors.white,),//padding: EdgeInsets.fromLTRB(0, edgeInsets.top, 0, edgeInsets.bottom),child: SafeArea(child: Column(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [Text("頂部內(nèi)容"),Text("底部內(nèi)容"),],),),),);} } Performing hot reload... Syncing files to device TAS AN00...======== Exception caught by widgets library ======================================================= The following assertion was thrown building MyApp(dirty): No MediaQuery widget ancestor found.MyApp widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: MyAppdirty The ownership chain for the affected widget is: "MyApp ← [root]"No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.The relevant error-causing widget was: MyApp file:///D:/002_Project/002_Android_Learn/flutter_screen_adaption/lib/main.dart:4:10 When the exception was thrown, this was the stack: #0 debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:219:7) #1 debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:234:4) #2 MediaQuery.of (package:flutter/src/widgets/media_query.dart:820:12) #3 MyApp.build (package:flutter_screen_adaption/main.dart:14:46) #4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28) ... ==================================================================================================== Reloaded 1 of 553 libraries in 299ms.二、解決方案
獲取 Padding 信息 ,
/// 獲取當前的 padding 信息 , 報錯位置 ; final EdgeInsets edgeInsets = MediaQuery.of(context).padding;需要在 MaterialApp 中進行獲取 , 這里將上述代碼重新進行封裝 ,
將 MediaQuery 調(diào)用的代碼 , 封裝到 MaterialApp 組件內(nèi)部 ;
import 'package:flutter/material.dart';/// 使用 MediaQuery 進行全面屏適配 void main() {runApp(MyApp()); }class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: HomePage(),);} }class HomePage extends StatelessWidget{@overrideWidget build(BuildContext context) {/// 獲取當前的 padding 信息final EdgeInsets edgeInsets = MediaQuery.of(context).padding;return Container(decoration: BoxDecoration(color: Colors.white,),padding: EdgeInsets.fromLTRB(0, edgeInsets.top, 0, edgeInsets.bottom),child: Column(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [Text("頂部內(nèi)容"),Text("底部內(nèi)容"),],),);} }此時報錯信息處理完畢 ;
總結
以上是生活随笔為你收集整理的【错误记录】Flutter 使用 MediaQuery 适配全面屏报错 ( No MediaQuery widget ancestor found. )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flutter】Flutter 启动白
- 下一篇: 【Flutter】屏幕像素适配方案 (