为什么从StatefulWidget添加TextFiled不会出现?

时间:2019-12-26 18:50:29

标签: flutter dart flutter-layout

我是Fultter的新手,并且遇到以下问题。 如果禁用ShowTextField(),我的以下代码可以正常工作。如果我启用ShowTextField(),则第几行将消失,并且某些TextField也不会出现。

1)可能是什么原因?

2)从StatelessWidget“调用” StateFulWidget是否正确?

预先感谢

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

class MyApp extends StatelessWidget {
  @override

  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter layout demo2',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter layout demo3'),
        ),
        body: Column(
          children: <Widget>[
            titleSection,
            ShowTextField(), //Disable this line will make code work.
          ],
        ),
      ),
    );
  }
}

class ShowTextField extends StatefulWidget {
  @override
  _ShowTextFieldState createState() => _ShowTextFieldState();
}

class _ShowTextFieldState extends State<ShowTextField> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Container(
          padding: EdgeInsets.all(20),
          color: Colors.red,
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Center(
              child: Column(
                children: <Widget>[
                  new TextField(
                    decoration: InputDecoration(
                      labelText: 'Hello world',
                    ),
                  ),
                ],
              ),
            ),
          ),
        )
      ],
    );
  }
}

1 个答案:

答案 0 :(得分:0)

1。)错误消息显示:“通常由TextField创建的InputDecorator不能具有无界的宽度。”然后这将起作用:

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter layout demo2',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter layout demo3'),
        ),
        body: Column(
          children: <Widget>[
            titleSection,
            ShowTextField(), //Disable this line will make code work.
          ],
        ),
      ),
    );
  }
}

class ShowTextField extends StatefulWidget {
  @override
  _ShowTextFieldState createState() => _ShowTextFieldState();
}

class _ShowTextFieldState extends State<ShowTextField> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Container(
          padding: EdgeInsets.all(20),
          color: Colors.red,
          child: Container(
        width: 300.0, //<--- SET THE WIDTH
            padding: const EdgeInsets.all(10),
            child: Center(
              child: Column(
                children: <Widget>[
                  new TextField(
                    decoration: InputDecoration(
                      labelText: 'Hello world',
                    ),
                  ),
                ],
              ),
            ),
          ),
        )
      ],
    );
  }
}

2。)是的,很好。