为什么不会从StatefulWidget添加TextFiled?

问题描述 投票:0回答:1

我是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',
                    ),
                  ),
                ],
              ),
            ),
          ),
        )
      ],
    );
  }
}

flutter dart flutter-layout
1个回答
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。)是的,很好。

© www.soinside.com 2019 - 2024. All rights reserved.