1

I have a layout effect, I don't know how to implement it more easily? I have six or N child widgets, placed in a parent widget with two child widgets per row, each widget is 50% the width of the parent widget and height is the height /rows of the parent widget.

I can use column, row expanded to do this, but I don't think it's simple enough.If my child widgets are intermediate, I don't know how to create them dynamically.

The layout effect what I want to achieve:

The way I want to do it is the following pseudocode I can do it in Android and iOS, but I don't know how to do it with flutter.

var parentWidget = Widget()

for (var i = 0; i < 6; i++) {
    var child = Widget()
    parentWidget.add(child)
}

The Flutter is implemented as follows. I can use column,row expanded to do this, but I don't think it's simple enough. If my child widgets are indeterminate, I don't know how to create them dynamically.

Expanded(
      child: Column(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Row(children: <Widget>[
              Expanded(child: Text("1"),),
              Expanded(child: Text("1"),),
            ],)
          ),
          Expanded(
            flex: 1,
            child:Row(children: <Widget>[
              Expanded(child: Text("1"),),
              Expanded(child: Text("1"),),
            ],),
          ),
          Expanded(
            flex: 1,
            child: Row(children: <Widget>[
              Expanded(child: Text("1"),),
              Expanded(child: Text("1"),),
            ],),
          )
        ],
      ),
    )
9
  • If you have many items, it's better to use ListView Commented Jan 15, 2019 at 2:44
  • @PhucTran yes,But I don't want the scrolling effect, I just want the child widgets to split the parent widget to fit different screen sizes, so there aren't too many children, maybe six, maybe eight Commented Jan 15, 2019 at 2:51
  • It's only scrollable if the number of items is big enough (some are out of screens) Commented Jan 15, 2019 at 2:53
  • Why not use a GridView? docs.flutter.io/flutter/widgets/GridView-class.html Commented Jan 15, 2019 at 5:52
  • stackoverflow.com/a/54079114/10269042 Commented Jan 15, 2019 at 6:25

1 Answer 1

0

I've come up with a way to deal with this requirement by calculating the width and height of each child widget and then placing them in wrap to arrange themselves


class RowFixedWrap extends StatefulWidget {
  final double spacing;
  final double runSpacing;
  final int columnCount;
  final List<Widget> childern;

  RowFixedWrap(
      {Key key, this.spacing, this.runSpacing, this.columnCount, this.childern})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _FixedWrapState();
  }
}

class _FixedWrapState extends State<RowFixedWrap> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        var itemWidth =
            (constraints.maxWidth - widget.spacing * (widget.columnCount - 1)) /
                widget.columnCount;
        var rows = widget.childern.length % widget.columnCount != 0
            ? (widget.childern.length ~/ widget.columnCount) + 1
            : (widget.childern.length ~/ widget.columnCount);
        var itemHeight =
            (constraints.maxHeight - widget.runSpacing * (rows - 1)) / rows;

        return Wrap(
            spacing: widget.spacing,
            runSpacing: widget.runSpacing,
            children: widget.childern.map((widget) {
              return SizedBox(
                width: itemWidth,
                height: itemHeight,
                child: widget,
              );
            }).toList());
      },
    );
  }
}
Expanded(
        child: Padding(
      padding: const EdgeInsets.all(10.0),
      child: RowFixedWrap(
        spacing: 10,
        runSpacing: 10,
        columnCount: 2,
        childern: <Widget>[
          Container(
            color: Colors.red,
          ),
          Container(
            color: Colors.red,
          ),
          Container(
            color: Colors.red,
          ),
          Container(
            color: Colors.red,
          ),
        ],
      ),
    ));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.