Fill an area with color in Flutter

后端 未结 1 944
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 20:08

I am using Flutter to develop an app and I have a row in a card with three items, I am try to fill the area for the last item with a color but there seems to be padding arou

相关标签:
1条回答
  • 2021-01-18 20:58

    The trick is to set on your Row crossAxisAlignment to CrossAxisAlignment.stretch. This will force all it's children to expand vertically. But then you have to add an height constraint or else the row will expand on the vertical axis.

    The easiest solution in your case is to wrap your Row into a SizedBox with a fixed height and unconstrained width. Where the height would be equal to ListTile height, as it's the biggest element of your list. So 56.0, considering you have a one line title and without dense property.

    Then you may want to align your Text inside the colored container. As it's top aligned instead of vertical centered. Which can be achieved with the alignment property on Container set to Alignment.center or Alignment.centerLeft/Right depending on your need.

    new Container(
      margin: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0),
      child: new InkWell(
        child: new Card(
          child: new SizedBox(
            height: 56.0,
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                new Expanded(
                  child: new ListTile(
                    title: new Text('foo'),
                  ),
                ),
                new Container(
                    color: Colors.blue,
                    alignment: Alignment.center,
                    padding: const EdgeInsets.symmetric(horizontal: 10.0),
                    child: new Text(
                      "foo",
                      style: new TextStyle(color: Colors.white),
                    ),
                ),
              ],
            ),
          ),
        ),
      ),
    )
    

    0 讨论(0)
提交回复
热议问题