Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Wrap widget inside Row widget

I want to achieve this behavior. I have 4 items in a Row but I want two texts in the middle act like the yare in a Wrap widget and text2 moves to next line if text1 is long and fill all spaces. enter image description here

Here is my code but it overflows instead of wrapping texts in two lines


  Widget _buildItem(String name, String status) {
    return Container(
      padding: const EdgeInsets.all(Dimens.unitX2),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Container(
            width: Dimens.unitX5,
            height: Dimens.unitX5,
            color: Colors.blue,
          ),
          SizedBox(width: Dimens.unitX1),
          Wrap(
            crossAxisAlignment: WrapCrossAlignment.center,
            alignment: WrapAlignment.spaceBetween,
            spacing: Dimens.unitX1,
            direction: Axis.horizontal,
            children: [
              Text(name),
              Text(status),
            ],
          ),
          SizedBox(width: Dimens.unitX1),
          Container(
            color: Colors.red,
            width: Dimens.unitX5,
            height: Dimens.unitX5,
          ),
        ],
      ),
    );
  }
like image 473
DNS Avatar asked Apr 20 '21 10:04

DNS


People also ask

What is wrap widget in Flutter?

A Wrap lays out each child and attempts to place the child adjacent to the previous child in the main axis, given by direction, leaving spacing space in between. If there is not enough space to fit the child, Wrap creates a new run adjacent to the existing children in the cross axis.

How do I show widgets from another widget in Flutter?

In Flutter, the overlay lets you print visual elements on top of other widgets by inserting them into the overlay's stack. You insert a widget into the overlay using an OverlayEntry and you use Positioned and AnimatedPositioned to choose where the entry is positioned within the overlay.

What is the use of the wrap widget in flutter?

Wrap widgets can support both Horizontal alignment and Vertical Alignment like Row and Column widgets in flutter. Using the Wrap widget we can adjust multiple widgets at once without disturbing each other. 1.

How to move row content automatically to next line in flutter?

Wrap widgets automatically align the Widgets items one by one and if the Row is filled then it will move row content automatically to next line in flutter. Wrap widgets can support both Horizontal alignment and Vertical Alignment like Row and Column widgets in flutter.

How does flutter place the children of a row widget?

To understand how Flutter places the children of a Row widget, you need to understand the layout algorithm. Layout each child a null or zero flex factor with unbounded horizontal constraints and the incoming vertical constraints.

What is the wrap widget?

The wrap widget is similar to Row or a Column widget with an added advantage that it can adjust its children according to the space available to it on the Screen. The default arrangement is horizontal (like a row) but you can make it vertical (like a column) as well by changing its direction property.


2 Answers

Wrap the Wrap widget in a Flexible:

...
Flexible(
    child: Wrap(
       crossAxisAlignment: WrapCrossAlignment.center,
       alignment: WrapAlignment.spaceBetween,
       spacing: 30,
       direction: Axis.horizontal,
       children: [
       Text('Text1'),
       Text('Text2 is a long text'),
     ],
   ),
 ),
...
like image 144
RaSha Avatar answered Nov 15 '22 07:11

RaSha


wrap your Wrap Widget with Expanded widget :

Widget _buildItem(String name, String status) {
    return Container(
        padding: const EdgeInsets.all(Dimens.unitX2),
        child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
                Container(
                    width: Dimens.unitX5,
                    height: Dimens.unitX5,
                    color: Colors.blue,
                ),
                SizedBox(width: Dimens.unitX1),
                Expanded(
                    child: Wrap(
                        crossAxisAlignment: WrapCrossAlignment.center,
                        alignment: WrapAlignment.spaceBetween,
                        spacing: Dimens.unitX1,
                        direction: Axis.horizontal,
                        children: [
                            Text(name),
                            Text(status),
                        ],
                    ),
                ),
                SizedBox(width: Dimens.unitX1),
                Container(
                    color: Colors.red,
                    width: Dimens.unitX5,
                    height: Dimens.unitX5,
                ),
            ],
        ),
    );
}
like image 36
DJafari Avatar answered Nov 15 '22 07:11

DJafari