未找到任何素材小部件

9 浏览
0 Comments

未找到任何素材小部件

我对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 final TextEditingController _controller = new TextEditingController();\n @override\n Widget build(BuildContext context) {\n return new Column(\n mainAxisAlignment: MainAxisAlignment.center,\n children: [\n new TextField(\n controller: _controller,\n decoration: new InputDecoration(\n hintText: \'Type something\',\n ),\n ),\n new RaisedButton(\n onPressed: () {\n showDialog(\n context: context,\n child: new AlertDialog(\n title: new Text(\'What you typed\'),\n content: new Text(_controller.text),\n ),\n );\n },\n child: new Text(\'DONE\'),\n ),\n ],\n );\n }\n}\n\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. (package:flutter/src/material/debug.dart:26)\nI/flutter ( 5187): #2 debugCheckHasMaterial (package:flutter/src/material/debug.dart:23)\nI/flutter ( 5187): #3 InputDecorator.build (package:flutter/src/material/input_decorator.dart:334)\n... \nI/flutter ( 5187): (忽略了来自_AssertionError类的一帧)\nI/flutter ( 5187): ════════════════════════════════════════════════════════════════════════════════════════════════════\nI/flutter ( 5187): 另一个异常被抛出:找不到Material小部件。\n

0
0 Comments

在我的情况下,我在Scaffold中使用了一个Hero小部件,就像这样:

Scaffold(

body:Hero(

child:ListView(

children:<Widget>[

TextField(),

...

...

]

)

)

);

我只是将Hero小部件移到了Scaffold之外,问题就解决了。

Hero(

child:Scaffold(

body:ListView(

children:<Widget>[

TextField(),

...

...

]

)

)

);

0
0 Comments

问题原因:没有找到Material widget。

解决方法:将需要包裹的widget用Material widget包裹起来。

代码示例:

Material(

child: InkWell(

onTap: onPressed,

),

);

0
0 Comments

"没有找到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小部件的存在。

0