使用TextField抛出"找不到材质小部件"错误

时间:2017-05-12 23:03:06

标签: flutter

我是新手,我试图执行示例here。我只想使用TextField小部件来获取一些用户输入。问题是我找到了一个" No Material小部件。"错误。我究竟做错了什么 ?谢谢。

代码:

./images/1/1.png
./images/1/2.png
./images/3/1.png
./images/3/2.png
./images/2/1.png
./images/2/2.png

这是错误堆栈:

 (881, 2079, 3)
 /home/xxxx/Desktop/stackoverflow/images/3/1.png
 3
 (155, 2552, 3)
 /home/xxxx/Desktop/stackoverflow/images/2/1.png
 2
 (562, 1978, 3)
 /home/xxxx/Desktop/stackoverflow/images/3/2.png
 3
 (291, 2558, 3)
 /home/xxxx/Desktop/stackoverflow/images/1/1.png
 1
 (157, 2554, 3)
 /home/xxxx/Desktop/stackoverflow/images/1/2.png
 1
 (866, 936, 3)
 /home/xxxx/Desktop/stackoverflow/images/2/2.png
 2

5 个答案:

答案 0 :(得分:28)

错误消息显示:

  

要介绍Material窗口小部件,您可以直接包含一个,   或者使用包含Material本身的小部件,例如Card,   DialogDrawerScaffold

在您的情况下,我可能会将Column包裹在Scaffold中。这样可以在以后轻松地向您的应用添加其他素材小部件,例如AppBarDrawerFloatingActionButton

@override
Widget build(BuildContext context) {
  return new Scaffold(
    body: 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'),
        ),
      ],
    ),
  );
}

答案 1 :(得分:2)

就我而言,我在这样的脚手架中使用了英雄小部件

Scaffold(
  body:Hero(
    child:ListView(
       children:<Widget>[
           TextField(),
           ...
           ...
           ]
          )
        )
       );

我只是将Hero Widget移出了脚手架,就解决了问题

Hero(
    child:Scaffold(
    body:ListView(
       children:<Widget>[
           TextField(),
           ...
           ...
           ]
          )
       ));

答案 2 :(得分:1)

将小部件放入支架中,如下所示:

    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Demo',
          //home: MyWidget(), --> do not do this !!!
          home: Home() --> this will wrap it in Scaffold
        );
      }
    }

    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('Demo'),
            ),
            body: MyWidget()); --> put your widget here
      }
    }

class MyWidget extends StatefulWidget {
...

答案 3 :(得分:0)

简单的解决方法是:

代替使用:

void main() {
  runApp(new MyApp());
}

您应该使用:

void main() => runApp(
  MaterialApp(
    home: MyApp()
  )
);

希望这对您有帮助!

答案 4 :(得分:0)

该错误表示您必须将列包装在Material小部件内,可以将其作为支架的主体放置,也可以作为Materialapp的宿主放置。例如:

 `return MaterialApp(
       home:  new Column(
       mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
       new TextField(
      controller: _controller,
      decoration: new InputDecoration(
        hintText: 'Type something',
      ),
    ),
      );`

`return Scaffold(
       body:  new Column(
       mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
       new TextField(
      controller: _controller,
      decoration: new InputDecoration(
        hintText: 'Type something',
      ),
    ),
      );``