我是Flutter的新手,我正在尝试执行here示例。我只是想使用TextField部件来获取一些用户输入。问题是我得到了一个“No Material widget found.”错误。我做错了什么?谢谢。
代码:
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new ExampleWidget(),
);
}
}
/// Opens an [AlertDialog] showing what the user typed.
class ExampleWidget extends StatefulWidget {
ExampleWidget({Key key}) : super(key: key);
@override
_ExampleWidgetState createState() => new _ExampleWidgetState();
}
/// State for [ExampleWidget] widgets.
class _ExampleWidgetState extends State<ExampleWidget> {
final TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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'),
),
],
);
}
}
这是错误堆栈:
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Built build/app/outputs/apk/app-debug.apk (21.5MB).
I/flutter ( 5187): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5187): The following assertion was thrown building InputDecorator(decoration: InputDecoration(hintText:
I/flutter ( 5187): "Type something"); baseStyle: null; isFocused: false; isEmpty: true; dirty):
I/flutter ( 5187): No Material widget found.
I/flutter ( 5187): InputDecorator widgets require a Material widget ancestor.
I/flutter ( 5187): In material design, most widgets are conceptually "printed" on a sheet of material. In Flutter's
I/flutter ( 5187): material library, that material is represented by the Material widget. It is the Material widget
I/flutter ( 5187): that renders ink splashes, for instance. Because of this, many material library widgets require that
I/flutter ( 5187): there be a Material widget in the tree above them.
I/flutter ( 5187): To introduce a Material widget, you can either directly include one, or use a widget that contains
I/flutter ( 5187): Material itself, such as a Card, Dialog, Drawer, or Scaffold.
I/flutter ( 5187): The specific widget that could not find a Material ancestor was:
I/flutter ( 5187): InputDecorator(decoration: InputDecoration(hintText: "Type something"); baseStyle: null;
I/flutter ( 5187): isFocused: false; isEmpty: true)
I/flutter ( 5187): The ownership chain for the affected widget is:
I/flutter ( 5187): InputDecorator ← AnimatedBuilder ← Listener ← _GestureSemantics ← RawGestureDetector ←
I/flutter ( 5187): GestureDetector ← TextField ← Column ← ExampleWidget ← _ModalScopeStatus ← ⋯
I/flutter ( 5187):
I/flutter ( 5187): When the exception was thrown, this was the stack:
I/flutter ( 5187): #0 debugCheckHasMaterial.<anonymous closure> (package:flutter/src/material/debug.dart:26)
I/flutter ( 5187): #2 debugCheckHasMaterial (package:flutter/src/material/debug.dart:23)
I/flutter ( 5187): #3 InputDecorator.build (package:flutter/src/material/input_decorator.dart:334)
... <output omitted>
I/flutter ( 5187): (elided one frame from class _AssertionError)
I/flutter ( 5187): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 5187): Another exception was thrown: No Material widget found.
9条答案
按热度按时间biswetbf1#
错误消息显示:
要引入
Material
小部件,可以直接包含一个小部件,也可以使用包含Material
本身的小部件,例如Card
、Dialog
、Drawer
或Scaffold
。对于您的情况,我可能会将您的
Column
封装在Scaffold
中,这样以后就可以轻松地将其他材质小部件添加到您的应用中,例如AppBar
、Drawer
或FloatingActionButton
。hlswsv352#
只要用**
Material widget
** Package 你的Widegt,它就解决了这个问题。rekjcdws3#
在我的例子中,我在一个脚手架中使用了一个英雄小部件,如下所示
我只是简单地将Hero小部件移到了Scaffold之外,它就解决了这个问题
vfwfrxfs4#
将您的小部件放入Scaffold中,如下所示:
jk9hmnmh5#
该错误意味着您必须将列 Package 在Material小部件中,您可以将其作为主体放置到scaffold中,或将其作为Materialapp的主目录放置。例如:
或
kyxcudwk6#
代替
InkWell
使用GestureDetector
小部件,对我来说修复了问题。gwo2fgha7#
这可能是由错误的路线造成的。
逐一检查您的路线。
kzmpq1sx8#
请尝试以下方法:只需将
builder
行添加到MaterialApp
:在这里你也可以提供一个典型的页面,其中包含
AppBar
,Body
,ScrollConfiguration
,Platform
和其他你想要的元素(你甚至可以添加全局监听器)。这非常方便:)并且沿着路由的任何页面都将被 Package 。例如,从传递给此方法的BuildContext中,Directionality、Localizations、DefaultTextStyle、MediaQuery等都是可用的。也可以通过影响Navigator或Router中所有路由的方式覆盖它们。
这很少有用,但是可以在希望覆盖那些默认值的应用中使用,例如强制应用进入从右到左模式,尽管是英语,或者覆盖MediaQuery度量(例如为来自OEM代码的插件显示的广告留下间隙)。
hlswsv359#
简单的解决办法是:
不使用:
您应该用途:
希望这对你有帮助!