IT박스

행 내부의 TextField로 인해 레이아웃 예외 발생 : 크기를 계산할 수 없음

itboxs 2020. 9. 9. 07:49
반응형

행 내부의 TextField로 인해 레이아웃 예외 발생 : 크기를 계산할 수 없음


해결 방법을 이해할 수없는 렌더링 예외가 발생합니다. 3 개의 행이있는 열을 만들려고합니다.

행 [이미지]

행 [TextField]

행 [버튼]

컨테이너를 빌드하는 코드는 다음과 같습니다.

Container buildEnterAppContainer(BuildContext context) {
    var container = new Container(
      padding: const EdgeInsets.all(8.0),
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          buildImageRow(context),
          buildAppEntryRow(context),
          buildButtonRow(context)
        ],
      ),
    );
    return container;
  }

텍스트 컨테이너에 대한 내 buildAppEntryRow 코드

Widget buildAppEntryRow(BuildContext context) {
    return new Row(
      children: <Widget>[
        new TextField(
          decoration: const InputDecoration(helperText: "Enter App ID"),
          style: Theme.of(context).textTheme.body1,
        )
      ],
    );
  }

실행할 때 다음 예외가 발생합니다.

I/flutter ( 7674): BoxConstraints forces an infinite width.
I/flutter ( 7674): These invalid constraints were provided to RenderStack's layout() function by the following
I/flutter ( 7674): function, which probably computed the invalid constraints in question:
I/flutter ( 7674):   RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:256:13)
I/flutter ( 7674): The offending constraints were:
I/flutter ( 7674):   BoxConstraints(w=Infinity, 0.0<=h<=Infinity)

buildAppEntryRow를 대신 TextField로 변경하면

 Widget buildAppEntryRow2(BuildContext context) {
    return new TextField(
      decoration: const InputDecoration(helperText: "Enter App ID"),
      style: Theme.of(context).textTheme.body1,
    );
  }

더 이상 예외가 발생하지 않습니다. 행 구현에서 해당 행의 크기를 계산할 수없는 원인이 무엇입니까?


( 앞으로 Row다른 위젯을 추가하고 싶기 때문에 a를 사용하고 있다고 가정합니다 TextField.)

Row위젯은이 유연한 사람 떠났다 것을 얼마나 많은 공간을 알 수 있도록 자사의 비 유연성 아이들의 고유 크기를 결정하고자합니다. 그러나 TextField본질적인 너비는 없습니다. 부모 컨테이너의 전체 너비에 맞게 크기를 조정하는 방법 만 알고 있습니다. A의 랩핑 시도 Flexible하거나 Expanded에게 Row당신이 기대하고 있다는 TextField남은 공간을 차지하기 :

      new Row(
        children: <Widget>[
          new Flexible(
            child: new TextField(
              decoration: const InputDecoration(helperText: "Enter App ID"),
              style: Theme.of(context).textTheme.body1,
            ),
          ),
        ],
      ),

행 내에서 Textfield를 사용하려면 Flexible을 사용해야합니다.

new Row(
              children: <Widget>[
                new Text("hi there"),
                new Container(
                  child:new Flexible(
                        child: new TextField( ),
                            ),//flexible
                ),//container


              ],//widget
            ),//row

TextField가로 방향으로 확장되고는 확장되므로이 오류가 발생 합니다 Row. 따라서의 너비를 제한해야하므로 TextField여러 가지 방법이 있습니다.

  1. 사용하다 Expanded

     Row(
      children: <Widget>[
        Expanded(child: TextField()),
        OtherWidget(),
      ],
    )
    
  2. 사용하다 Flexible

    Row(
      children: <Widget>[
        Flexible(child: TextField()),
        OtherWidget(),
      ],
    )
    
  3. 그것을 포장 Container하거나 SizedBox제공하십시오width

    Row(
      children: <Widget>[
        SizedBox(width: 100, child: TextField()),
        OtherWidget(),
      ],
    )       
    

As @Asif Shiraz mentioned I had same issue and solved this by Wrapping Column in a Flexible, here like this,,

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Flutter Demo',
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new Scaffold(
          body: Row(
            children: <Widget>[
              Flexible(
                  child: Column(
                children: <Widget>[
                  Container(
                    child: TextField(),
                  )
                  //container
                ],
              ))
            ],
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
          ),
        ));
  }
}

A simple solution is to wrap your Text() inside a Container(). So, your code will be like:

Container(
      child: TextField()
)

Here you also get the width and height attribute of a container to adjust the look and feel of your text field. No need to use Flexible if you are wrapping your text field inside of a Container.


The solution is to wrap your Text() inside one of the following widgets: Either Expanded or Flexible. So, your code using Expanded will be like:

Expanded(
           child: TextField(
             decoration: InputDecoration(
               hintText: "Demo Text",
               hintStyle: TextStyle(fontWeight: FontWeight.w300, color: Colors.red)
              ),
           ),
        ),

참고URL : https://stackoverflow.com/questions/45986093/textfield-inside-of-row-causes-layout-exception-unable-to-calculate-size

반응형