23

How do I get two side-by-side Text widgets to only allow the left-most one to overflow gracefully?

One text widget in a column is easy to overflow gracefully. Two text widgets side-by-side in a row is problematic.

Stack overflow wants more details but honestly, I'm probably just wasting more of your time at this point.

Widget _listItem({String title, String subtitle}){
  return Row(
    children: <Widget>[
      _listItemIcon(),
      Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _listItemTitle(title),
            Row(
              children: <Widget>[
                _listItemSubtitle(subtitle), // how do I allow this to overflow gracefully
                _listItemTime("2m") // but stop this from overflowing
              ],
            )
          ],
        ),
      ),
      _listItemFavorite()
    ],
  );
}

//////////////
Widget _listItemIcon(){
  return Icon(Icons.message);
}

Widget _listItemTitle(String title){
  return Text(
    title,
    softWrap: false,
    overflow: TextOverflow.fade,
    style: TextStyle(
      fontSize:24.0
    ),
  );
}

Widget _listItemSubtitle(String subtitle){
  return Text(
    "[$subtitle]",
    softWrap: false,
    overflow: TextOverflow.fade,
    );
}

Widget _listItemTime(String time){
  return Text(
    "[$time]",
    softWrap: false,
    overflow: TextOverflow.fade,
    );
}

Widget _listItemFavorite(){
  return Icon(Icons.favorite);
}

enter image description here

Luke Pighetti
  • 4,541
  • 7
  • 32
  • 57

2 Answers2

55

You can use the Flexible widget:

Widget _listItemSubtitle(String subtitle) {
  return new Flexible(
    fit: FlexFit.loose,
    child: Text(
      "[$subtitle]",
      softWrap: false,
      overflow: TextOverflow.fade,
    ),
  );
}

result


Alternative:

If you prefer that the time widget is always on the right, you could wrap the subtitle text in an Expanded widget:

Widget _listItemSubtitle(String subtitle){
  return Expanded(
    child: Text(
      "[$subtitle]",
      softWrap: false,
      overflow: TextOverflow.fade,
    ),
  );
}

result

boformer
  • 28,207
  • 10
  • 81
  • 66
0

Sometimes we have a lot of different widgets in the Row, in this case, we can use this approach

        child: SingleChildScrollView(
          physics: const NeverScrollableScrollPhysics(),
          scrollDirection: Axis.horizontal,
          child: Row(
            children: items,
          ),
        ),
awaik
  • 10,143
  • 2
  • 44
  • 50