未找到任何素材小部件
未找到任何素材小部件
我对Flutter还不熟悉,我试图执行这个例子here。我只想使用TextField小部件来获取用户输入。问题是我得到了一个\"找不到Material小部件\"的错误。我做错了什么?谢谢。\n代码:\nimport \'package:flutter/material.dart\'; \nvoid main() {\n runApp(new MyApp());\n}\nclass MyApp extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n return new MaterialApp(\n title: \'Flutter Demo\',\n home: new ExampleWidget(),\n );\n }\n}\nclass ExampleWidget extends StatefulWidget {\n ExampleWidget({Key key}) : super(key: key);\n @override\n _ExampleWidgetState createState() => new _ExampleWidgetState();\n}\nclass _ExampleWidgetState extends State
\n这是错误堆栈:\nLaunching lib/main.dart on Android SDK built for x86 in debug mode...\nBuilt build/app/outputs/apk/app-debug.apk (21.5MB).\nI/flutter ( 5187): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════\nI/flutter ( 5187): 在构建InputDecorator(decoration: InputDecoration(hintText: \"Type something\"); baseStyle: null; isFocused: false; isEmpty: true; dirty)时发生了以下断言错误:\nI/flutter ( 5187): 找不到Material小部件。\nI/flutter ( 5187): InputDecorator小部件需要Material小部件的祖先。\nI/flutter ( 5187): 在Material设计中,大多数小部件在概念上都是在材料的一张纸上\"打印\"的。在Flutter的材料库中,这种材料由Material小部件表示。例如,它是Material小部件渲染墨水涟漪的地方。因此,许多材料库小部件要求在它们之上的树中有一个Material小部件。\nI/flutter ( 5187): 要引入一个Material小部件,你可以直接包含一个,或者使用一个包含Material本身的小部件,例如Card,Dialog,Drawer或Scaffold。\nI/flutter ( 5187): 无法找到Material祖先的特定小部件是:\nI/flutter ( 5187): InputDecorator(decoration: InputDecoration(hintText: \"Type something\"); baseStyle: null; isFocused: false; isEmpty: true)\nI/flutter ( 5187): 受影响小部件的所有权链是:\nI/flutter ( 5187): InputDecorator ← AnimatedBuilder ← Listener ← _GestureSemantics ← RawGestureDetector ← GestureDetector ← TextField ← Column ← ExampleWidget ← _ModalScopeStatus ← ⋯\nI/flutter ( 5187): \nI/flutter ( 5187): 当抛出异常时,这是堆栈:\nI/flutter ( 5187): #0 debugCheckHasMaterial.
"没有找到Material小部件"这个问题的出现原因是没有在Widget树中包含Material小部件。解决方法是将Column小部件包装在Scaffold小部件中,这样可以轻松地在应用程序中添加其他Material小部件,比如AppBar、Drawer或FloatingActionButton。
在这种方法中的挑战是新页面不包含底部导航栏或应用程序的主题,我们能在同一个应用程序中有两个MaterialApps吗?
以下是解决问题的代码示例:
Widget build(BuildContext context) {
return new Scaffold(
body: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
new TextField(
controller: _controller,
decoration: new InputDecoration(
hintText: 'Type something',
),
),
new RaisedButton(
onPressed: () {
showDialog(
context: context,
child: new AlertDialog(
title: new Text('What you typed'),
content: new Text(_controller.text),
),
);
},
child: new Text('DONE'),
),
],
),
);
}
以上代码将Column小部件包装在Scaffold小部件中,确保了Material小部件的存在。