颤动 - 如何使小部件填充列中的剩余空间

Gus*_*ter 14 flutter

在Android中,我们可以执行以下操作,ImageView以根据大小来填充尽可能多的空间TextView.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我们如何实现这一目标Flutter?假设我需要Datetime尽可能地填充(绿色).

new Column(
children: <Widget>[
    new Text('Title',
        style: new TextStyle(fontWeight: FontWeight.bold)
    ),
    new Text('Datetime',
        style: new TextStyle(color: Colors.grey)
    ),
],
Run Code Online (Sandbox Code Playgroud)

)

在此输入图像描述

Kev*_*ter 43

不是100%肯定,但我认为你的意思是这个.Expanded小部件扩展到它可以使用的空间.虽然我认为它在行或列中的行为有点不同.

new Column(
children: <Widget>[
    new Text('Title',
        style: new TextStyle(fontWeight: FontWeight.bold)
    ),
    new Expanded(
        child: new Text('Datetime',
             style: new TextStyle(color: Colors.grey)
        ),
    ),
],
)
Run Code Online (Sandbox Code Playgroud)


Cop*_*oad 10

您可以使用:

  1. ExpandedAlign定位孩子

    Column(
      children: [
        FlutterLogo(),
        Expanded( // add this
          child: Align(
            alignment: Alignment.bottomCenter,
            child: FlutterLogo(),
          ),
        ),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)
  2. Spacer

    Column(
      children: [
        FlutterLogo(),
        Spacer(), // add this
        FlutterLogo(),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)
  3. mainAxisAlignment

    Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween, // add this
      children: [
        FlutterLogo(colors: Colors.orange),
        FlutterLogo(colors: Colors.blue),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)


小智 8

如果您只需要添加空格,请使用 Spacer()

new Column(
children: <Widget>[
    new Text('Title',
        style: new TextStyle(fontWeight: FontWeight.bold)
    ),
    new Text('Datetime',
        style: new TextStyle(color: Colors.grey)
    ),
    Spacer(),
],
Run Code Online (Sandbox Code Playgroud)