You need to enable JavaScript to run this app. Flutter已引入MaterialApp仍报No MediaQuery widget ancestor found解决
优惠活动
大模型
产品
解决方案
定价
更多

Flutter已引入MaterialApp仍报No MediaQuery widget ancestor found解决

错误原因

你当前传入showModalBottomSheet的context来自MyApp的build方法,该上下文对应的组件节点位于MaterialApp的上层。而MediaQuery是MaterialApp内部自动挂载的组件,从上层context向上查找自然找不到对应的祖先节点,所以抛出了该错误。你参考的教程可以正常运行,通常是因为教程将Scaffold部分拆分为了独立的子组件,使用的是MaterialApp下层的上下文。

解决方法

有两种常用的修复方案:

方案1:将Scaffold抽为独立组件

将MaterialApp的home部分拆分为单独的StatefulWidget或StatelessWidget,此时子组件的build方法返回的context天然处于MaterialApp下层,可以正常获取MediaQuery:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App name',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  List<info> li = [
    info(name: 'text1', length: 170, date: DateTime.now()),
    info(name: 'text2', length: 175, date: DateTime.now()),
    info(name: 'text3', length: 180, date: DateTime.now()),
    info(name: 'text4', length: 180, date: DateTime.now()),
    info(name: 'text5', length: 180, date: DateTime.now()),
    info(name: 'text6', length: 180, date: DateTime.now()),
    info(name: 'text7', length: 180, date: DateTime.now()),
    info(name: 'text8', length: 180, date: DateTime.now()),
    info(name: 'text9', length: 180, date: DateTime.now()),
  ];

  void x (BuildContext ctx){
    showModalBottomSheet(context: ctx, builder: (ctx){
      return ListView.builder(
        itemCount: li.length,
        itemBuilder: (cx , index){
          return Padding(
            padding: EdgeInsets.all(10.0),
            child: Card(
              shadowColor: Colors.red,
              elevation: 10.0,
              color: Colors.blue,
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text(
                          li[index].name,
                          style: TextStyle(color: Colors.white, fontSize: 20),
                        ),
                        Text(
                          '${li[index].length}',
                          style: TextStyle(color: Colors.white, fontSize: 20),
                        ),
                      ],
                    ),
                    Text(
                      '${li[index].date}',
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('This is the App bar'),
      ),
      body: Container(
        padding: EdgeInsets.all(10.0),
        height: double.infinity,
        color: Colors.black,
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () => x(context)
      ),
    );
  }
}

方案2:用Builder包裹Scaffold

如果不想拆分组件,可以直接在MaterialApp的home属性处用Builder包裹Scaffold,Builder会生成位于MaterialApp下层的新context:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'App name',
    home: Builder(
      builder: (newContext) => Scaffold(
        appBar: AppBar(
          title: Text('This is the App bar'),
        ),
        body: Container(
          padding: EdgeInsets.all(10.0),
          height: double.infinity,
          color: Colors.black,
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () => x(newContext)
        ),
      ),
    ),
  );
}

内容的提问来源于stack exchange,提问作者Miran GAMER

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 12:54:04