Android 手势捕捉到的颤振异常。在showModalBottomSheet中未找到MediaQuery小部件

Android 手势捕捉到的颤振异常。在showModalBottomSheet中未找到MediaQuery小部件,android,flutter,dart,Android,Flutter,Dart,为什么我不能在浮动操作按钮中使用showModalBottomSheet?它只是不断向我显示这个错误: I/flutter (16368): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter (16368): The following assertion was thrown while handling a gesture

为什么我不能在浮动操作按钮中使用showModalBottomSheet?它只是不断向我显示这个错误:

I/flutter (16368): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (16368): The following assertion was thrown while handling a gesture:
I/flutter (16368): No MediaQuery widget found.
I/flutter (16368): MyApp widgets require a MediaQuery widget ancestor.
I/flutter (16368): The specific widget that could not find a MediaQuery ancestor was:
I/flutter (16368):   MyApp
I/flutter (16368): The ownership chain for the affected widget is: "MyApp ← [root]"
I/flutter (16368): Typically, the MediaQuery widget is introduced by the MaterialApp or WidgetsApp widget at the top of
I/flutter (16368): your application widget tree.
I/flutter (16368): 
I/flutter (16368): When the exception was thrown, this was the stack:
I/flutter (16368): #0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:211:7)
I/flutter (16368): #1      debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:223:4)
I/flutter (16368): #2      showModalBottomSheet (package:flutter/src/material/bottom_sheet.dart:469:10)
I/flutter (16368): #3      _MyAppState.build.<anonymous closure> (package:flutter_happy_habits/main.dart:32:29)
I/flutter (16368): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
I/颤振(16368):══╡ 用手势捕捉异常╞═══════════════════════════════════════════════════════════════════
I/flatter(16368):在处理手势时抛出以下断言:
I/flatter(16368):未找到MediaQuery小部件。
I/flatter(16368):MyApp小部件需要MediaQuery小部件祖先。
I/flatter(16368):找不到MediaQuery祖先的特定小部件是:
I/颤振(16368):MyApp
I/flatter(16368):受影响小部件的所有权链是:“MyApp”← [根]”
I/flatter(16368):通常,MediaQuery小部件由位于
I/flatter(16368):您的应用程序小部件树。
I/颤振(16368):
I/flatter(16368):当抛出异常时,这是堆栈:
I/flatter(16368):#0 debugCheckHasMediaQuery。(包:flatter/src/widgets/debug.dart:211:7)
I/flatter(16368):#1 debugCheckHasMediaQuery(包:flatter/src/widgets/debug.dart:223:4)
I/颤振(16368):#2显示模型底板(包装:颤振/src/材料/底板。省道:469:10)
I/flatter(16368):#3_MyAppState.build。(套餐:flatter\u happy\u habits/main.飞镖:32:29)
I/颤振(16368):#4(InkResponse Estate.)(handleTap)(包装:颤振/src/材料/墨水(well.省道:706:14)
导入“包装:颤振/材料.省道”;
导入“./models/home.dart”;
导入“models/progress.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
int _selectedPage=0;
最终页面选项=[
Home(),
进度(),
进度(),
];
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
debugShowCheckedModeBanner:true,
家:新脚手架(
appBar:appBar(标题:文本(“颤振演示”),
正文:_pageOptions[_selectedPage],
floatingActionButtonLocation:floatingActionButtonLocation.centerDocked,
浮动操作按钮:浮动操作按钮(
子:图标(Icons.add),
onPressed:(){showModalBottomSheet(
上下文:上下文,
生成器:(上下文){
返回文本('Modal bottom sheet',样式:TextStyle(fontSize:30));
});
}
),
bottomNavigationBar:BottomAppBar(
形状:CircularNotchedRectangle(),
notchMargin:4.0,
孩子:新的一排(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.spaceAround,
儿童:[
图标按钮(
图标:图标(Icons.home),
已按下:(){
打印(“主页”);
设置状态(){
_selectedPage=0;
});
},
),
图标按钮(
图标:图标(图标。插入图表),
已按下:(){
打印(“进度”);
设置状态(){
_selectedPage=1;
});
},
),
],
),
),
),
);
}
}

这是因为,
showModalBottomSheet
试图从给定的
上下文访问类型
MaterialApp
的祖先

使用
Builder
widget获得新的
context
MaterialApp
祖先,或者将
MaterialApp
Scaffold
widget分离为单独的widget

使用
Builder

floatingActionButton: Builder(
  builder: (context) => FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () { showModalBottomSheet(
          context: context,
          builder: (context) {
            return Text('Modal bottom sheet', style: TextStyle(fontSize: 30));
          });
      }
  ),
),

我有一个解决办法。我不知道这是不是最好的,但它很管用。showModalBottomSheet不应与materialapp具有相同的上下文,因此必须在无状态小部件中进行分隔,如本例所示

导入“包装:颤振/材料.省道”;
void main()=>runApp(新的MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
标题:“颤振应用程序”,
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
家:新脚手架(
appBar:新的appBar(标题:常量文本(“模式底页”),
正文:新中心(
孩子:新升起的按钮(
子项:常量文本('显示底页'),
已按下:(){
showModalBottomSheet(上下文:context,builder:(BuildContext){
退回新货柜(
孩子:新的填充物(
填充:常数边集全部(32.0),
子项:新文本('这是模式的底部工作表。单击任意位置可忽略',
textAlign:textAlign.center,
样式:新文本样式(
颜色:主题。背景。强调颜色,
字体大小:24.0
)
)
)
);
});
}
)
)
)
);
}

}
但为什么?为什么我们需要
生成器
?我读了一些教程,没有人提到这一点。如下所示:@Gabriel当您调用
showModalBottomSheet()
时,您将传递一个
context
对象。此上下文用于查找祖先,即
MaterialApp
@Gabrie
floatingActionButton: Builder(
  builder: (context) => FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () { showModalBottomSheet(
          context: context,
          builder: (context) {
            return Text('Modal bottom sheet', style: TextStyle(fontSize: 30));
          });
      }
  ),
),