在 showModalBottomSheet 内部没有找到 MediaQuery 组件的错误通常是由于在 showModalBottomSheet 方法中没有正确设置 builder 参数导致的。下面是一个示例代码,展示了如何正确使用 showModalBottomSheet 方法和 builder 参数:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Demo'),
),
body: Center(
child: RaisedButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
child: Column(
children: [
Text('Modal Bottom Sheet'),
RaisedButton(
child: Text('Close'),
onPressed: () {
Navigator.pop(context);
},
),
],
),
);
},
);
},
child: Text('Show Modal Bottom Sheet'),
),
),
),
);
}
}
在这个示例中,我们在 RaisedButton 的 onPressed 回调函数中使用了 showModalBottomSheet 方法来显示一个模态底部弹出窗口。在 builder 参数中,我们返回了一个包含文本和一个关闭按钮的容器。当关闭按钮被按下时,我们使用 Navigator.pop 方法来关闭模态底部弹出窗口。
请确保在 showModalBottomSheet 的 builder 参数中返回一个包含 MediaQuery 组件的 Widget,以便正确地构建界面并避免出现异常。