在Flutter中的列内插入行

时间:2019-03-19 12:32:15

标签: flutter flutter-layout

我的UI屏幕基本上是使用Column Widget渲染的,并且在此Widget中,我将插入所有其他UI组件。其中之一恰好是行(由2个文本字段组成)。这是行小部件:

var phoneNumber = new Row(
  children: <Widget>[
    new Padding(
      padding: const EdgeInsets.all(20.0),
      child: countryCodePicker,
    ),
    new Padding(
      padding: const EdgeInsets.all(20.0),
      child: mobileNumber,
    ),
  ],
);

主UI屏幕为:

     return SafeArea(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
                customerName,
                emailAddress,
//                new Container(
//                  child: deliveryOptionRow,
//                ),
                mobileNumber,
                countryCodePicker,
                templateMessagesDropDown,
                templateMessageTextField,
                sendGoogleReview,
              ],
            )
    );

将行添加到列中时会看到此异常

     The following RenderObject was being processed when the exception was fired:
flutter:   _RenderListTile#06b03 relayoutBoundary=up11 NEEDS-LAYOUT NEEDS-PAINT
flutter:   creator: _ListTile ← MediaQuery ← Padding ← SafeArea ← Semantics ← Listener ← _GestureSemantics ←
flutter:   RawGestureDetector ← GestureDetector ← InkWell ← ListTile ← ListTileTheme ← ⋯
flutter:   parentData: offset=Offset(0.0, 0.0) (can use size)
flutter:   constraints: BoxConstraints(unconstrained)
flutter:   size: MISSING
flutter: This RenderObject had the following descendants (showing up to depth 5):
flutter:   _RenderRadio#302a4 relayoutBoundary=up12 NEEDS-PAINT
flutter:   RenderParagraph#58a45 NEEDS-LAYOUT NEEDS-PAINT

在主UI屏幕内添加行的最佳方法是什么。

2 个答案:

答案 0 :(得分:3)

这应该可以解决您的问题,

var phoneNumber = new Row(
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
    Expanded(
      child: new Padding(
        padding: const EdgeInsets.all(20.0),
        child: countryCodePicker,
      ),
    ),
    Expanded(
      child: new Padding(
        padding: const EdgeInsets.all(20.0),
        child: mobileNumber,
      ),
    ),
  ],
);

希望有帮助!

答案 1 :(得分:2)

如果您的 Row 包含 TextFieldTextFormField 或任何其他试图增长到无穷大的小部件,您需要为其提供内在宽度。您可以通过多种方式实现:

  • 使用Expanded

    Column(
      children: [
        Row(
          children: [
            Expanded( 
              child: TextField(), // <-- Wrapped in Expanded.
            ),
          ],
        ),
      ],
    )
    
  • 使用Flexible

    Column(
      children: [
        Row(
          children: [
            Flexible( 
              child: TextField(), // <-- Wrapped in Flexible.
            ),
          ],
        ),
      ],
    )
    
  • 提供固定宽度

    Column(
      children: [
        Row(
          children: [
            SizedBox(
              width: 200, // <-- Fixed width.
              child: TextField(),
            ),
          ],
        ),
      ],
    )