import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'info.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
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 MaterialApp(
title: 'App name',
home: 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)
),
),
);
}
}
错误:
在处理手势时抛出了以下断言: 找不到 MediaQuery 祖先小部件。
MyApp 小部件需要MediaQuery祖先小部件。找不到 MediaQuery 祖先的具体小部件是:MyApp 状态: MyAppState#7e07c 受影响小部件的所有权链为:“MyApp ← [root]”
从传递给 MediaQuery.of()的上下文开始找不到 MediaQuery 祖先。这可能是因为您尚未添加WidgetsApp、CupertinoApp或MaterialApp小部件(这些小部件引入了MediaQuery),或者如果您使用的上下文来自上述那些小部件之上的小部件,则可能发生这种情况。
我的代码有什么问题吗?我已经使用了 scaffold 和 MaterialApp 小部件,但讲师没有使用 MediaQuery,我甚至不知道那是什么意思,但他却可以正常工作!