해결 방법을 이해하지 못하는 렌더링 예외가 발생합니다. 3 행이있는 열을 만들려고합니다.
행 [이미지]
행 [텍스트 필드]
행 [버튼]
컨테이너를 빌드하는 코드는 다음과 같습니다.
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
고유 너비는 없습니다. 부모 컨테이너의 전체 너비로 크기를 조정하는 방법 만 알고 있습니다. 에 포장 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
가로 방향으로 확장되고이 때문에이 오류가 발생하므로 Row
의 너비를 제한해야 TextField
합니다. 여러 가지 방법이 있습니다.
-
사용하다
Expanded
Row( children: <Widget>[ Expanded(child: TextField()), OtherWidget(), ], )
-
사용하다
Flexible
Row( children: <Widget>[ Flexible(child: TextField()), OtherWidget(), ], )
-
그것을 감싸
Container
거나SizedBox
제공하십시오width
Row( children: <Widget>[ SizedBox(width: 100, child: TextField()), OtherWidget(), ], )
답변
행 내에서 텍스트 필드를 사용하려면 융통성을 사용해야합니다.
new Row(
children: <Widget>[
new Text("hi there"),
new Container(
child:new Flexible(
child: new TextField( ),
),//flexible
),//container
],//widget
),//row
답변
해결책은 Text()
내부 Expanded
또는 다음 위젯 중 하나 를 감싸는 것 Flexible
입니다. 확장을 사용하는 코드는 다음과 같습니다.
Expanded(
child: TextField(
decoration: InputDecoration(
hintText: "Demo Text",
hintStyle: TextStyle(fontWeight: FontWeight.w300, color: Colors.red)
),
),
),
답변
@Asif Shiraz가 언급했듯이 똑같은 문제가 있고 Flexible에 Wrapping Column을 사용 하여이 문제를 해결했습니다.
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,
),
));
}
}
답변
간단한 해결책은 Text()
내부 를 감싸는 것 Container()
입니다. 따라서 코드는 다음과 같습니다.
Container(
child: TextField()
)
여기에서 컨테이너의 너비 및 높이 속성을 가져와 텍스트 필드의 모양과 느낌을 조정할 수 있습니다. Flexible
컨테이너 내부에 텍스트 필드를 래핑 하는 경우 사용할 필요가 없습니다 .