为什么将TextField放入行中时出现错误

时间:2019-03-22 10:36:34

标签: dart flutter

我试图将两个文本字段排成一行,以便它们对齐成一行,但是我似乎遇到了以下错误,在这里我似乎无法理解我在做什么。

导入“ package:flutter / material.dart”;

void main(){
  runApp(
      MaterialApp(
          title: 'Planet',
          home:Home()
      )
  );
}

class Home extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HomeState();
  }

}


class HomeState extends State<Home>{

  @override
  Widget build(BuildContext context){

    return Scaffold(
      appBar: AppBar(
        title: Text(
            'Weight on Planet x',
        ),
        centerTitle: true,
      ),
      body:Container(
        alignment: Alignment.topCenter,
        child: ListView(
          padding: const EdgeInsets.all(2.5),
          children: <Widget>[
            Image.asset('images/planet.png',height: 133.0,width: 200.0,),
            Container(
              margin: const EdgeInsets.all(3.0),
              child: Row(
                children: <Widget>[
                  TextField(
                    controller: null,
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                        labelText: 'Your weight on Earth',
                        hintText: 'In Pounds',
                        icon: Icon(Icons.person_outline)
                    ),
                  ),
                  TextField(
                    controller: null,
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                        labelText: 'Your weight on Earth',
                        hintText: 'In Pounds',
                        icon: Icon(Icons.person_outline)
                    ),
                  )
                ],
              ),

            )
          ],
        ),
      )
    );
  }

我收到以下错误

引发了另一个异常:未布置RenderBox:_RenderDecoration#3373c relayoutBoundary = up10 NEEDS-PAINT

2 个答案:

答案 0 :(得分:2)

尝试将有限的EINTR赋予widthTextField。默认情况下,Container会占据其父级提供的全部空间,在这种情况下是无限的。

或者,如果您希望每个TextFields具有自定义宽度,则可以使用每个Containers

答案 1 :(得分:0)

我建议您使用Expanded来告知水平尺寸(可用的最大值)。

             Expanded(
              child: Row(
                children: <Widget>[
                  TextField(
                    controller: null,
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                        labelText: 'Your weight on Earth',
                        hintText: 'In Pounds',
                        icon: Icon(Icons.person_outline)),
                  ),
                  TextField(
                    controller: null,
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                        labelText: 'Your weight on Earth',
                        hintText: 'In Pounds',
                        icon: Icon(Icons.person_outline)),
                  )
                ],
              ),
            );