gpt4 book ai didi

Flutter:带有 TextField/TextFormField 的 Bottom Sheet

转载 作者:IT王子 更新时间:2023-10-29 06:53:10 26 4
gpt4 key购买 nike

在我们的应用程序的一部分中,我希望在 BottomSheet 中有一个简单的表单,就像下面的代码一样。不幸的是,当我输入它时,我得到了一个错误

The following assertion was thrown during performLayout(): AnInputDecorator, which is typically created by a TextField, cannot havean unbounded width. This happens when the parent widget does notprovide a finite width constraint. For example, if the InputDecoratoris contained by a Row, then its width must be constrained. An Expandedwidget or a SizedBox can be used to constrain the width of theInputDecorator or the TextField that contains it.'package:flutter/src/material/input_decorator.dart': Failed assertion:line 881 pos 7: 'layoutConstraints.maxWidth < double.infinity'

我实现的代码:

  void _settingModalBottomSheet(context){
showModalBottomSheet(
context: context,
elevation: 8.0,
builder: (BuildContext bc){
return Directionality(
textDirection: TextDirection.rtl,
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: 250.0
),
child: Container(
padding: EdgeInsets.fromLTRB(0.0,10.0,0.0,10.0),
child: new Wrap(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text(
'please fill this form',
style: TextStyle(
fontSize: 13.0,
),
),
),
),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
children: <Widget>[
Text('item 1'),
Container(
child: TextField(),
)
],
),
Column(
children: <Widget>[
Text('item 2'),
Container(
child: TextField(),
)
],
),

],
),
],
),
),
),
);
}
);
}

最佳答案

您必须为 TextField 提供特定宽度,只需在您的 Container 中提供 width 或包装您的 ColumnExpanded 中。

解决方案一

Container(
width: 100, // do it in both Container
child: TextField(),
),

方案二

Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded( // wrap your Column in Expanded
child: Column(
children: <Widget>[
Text('item 1'),
Container(child: TextField()),
],
),
),
Expanded( // wrap your Column in Expanded
child: Column(
children: <Widget>[
Text('item 2'),
Container(child: TextField()),
],
),
),
],
),

关于Flutter:带有 TextField/TextFormField 的 Bottom Sheet ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com