Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

reorderables

reorderable table, row, column, wrap, and sliver list that allow drag and drop of the children.

various reorderable, a.k.a. drag and drop, flutter widgets, including reorderable table, row, column, wrap, and sliver list, that make their children draggable and reorder them within the widget. parent widget only need to provide an onreorder function that is invoked with the old and new indexes of child being reordered.

usage

to use this package, add reorderables as a dependency in your pubspec.yaml file.

dependencies:
  reorderables:

and import the package in your code.

import 'package:reorderables/reorderables.dart';

examples

this package includes reorderablesliverlist, reorderabletable, reorderablewrap, reorderablerow, and reorderablecolumn, which are reorderable versions of flutter’s sliverlist, table, wrap, row, and column respectively.







reorderablesliverlist

reorderablesliverlist behaves exactly like sliverlist, but its children are draggable.

to make a sliverlist reorderable, replace it with reorderablesliverlist and replace sliverchildlistdelegate/sliverchildbuilderdelegate with reorderablesliverchildlistdelegate/reorderablesliverchildbuilderdelegate.
do make sure that there’s a scrollcontroller attached to the scrollview that contains reorderablesliverlist, otherwise an error will be thrown when dragging list items.

class _sliverexamplestate extends state<sliverexample> {
  list<widget> _rows;

  @override
  void initstate() {
    super.initstate();
    _rows = list<widget>.generate(50,
        (int index) => text('this is sliver child $index', textscalefactor: 2)
    );
  }

  @override
  widget build(buildcontext context) {
    void _onreorder(int oldindex, int newindex) {
      setstate(() {
        widget row = _rows.removeat(oldindex);
        _rows.insert(newindex, row);
      });
    }
    // make sure there is a scroll controller attached to the scroll view that contains reorderablesliverlist.
    // otherwise an error will be thrown.
    scrollcontroller _scrollcontroller = primaryscrollcontroller.of(context) ?? scrollcontroller();

    return customscrollview(
      // a scrollcontroller must be included in customscrollview, otherwise
      // reorderablesliverlist wouldn't work
      controller: _scrollcontroller,
      slivers: <widget>[
        sliverappbar(
          expandedheight: 210.0,
          flexiblespace: flexiblespacebar(
            title: text('reorderablesliverlist'),
            background: image.network(
              'https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/yushan'
                '_main_east_peak%2bhuang_chung_yu%e9%bb%83%e4%b8%ad%e4%bd%91%2b'
                '9030.png/640px-yushan_main_east_peak%2bhuang_chung_yu%e9%bb%83'
                '%e4%b8%ad%e4%bd%91%2b9030.png'),
          ),
        ),
        reorderablesliverlist(
          delegate: reorderablesliverchildlistdelegate(_rows),
          // or use reorderablesliverchildbuilderdelegate if needed
//          delegate: reorderablesliverchildbuilderdelegate(
//            (buildcontext context, int index) => _rows[index],
//            childcount: _rows.length
//          ),
          onreorder: _onreorder,
        )
      ],
    );
  }
}

reorderablesliverlist demo

reorderabletable

the difference between table and list is that cells in a table are horizontally aligned, whereas in a list, each item can have children but they are not aligned with children in another item.

making a row draggable requires cells to be contained in a single widget. this isn’t achievable with table or gridview widget since their children are laid out as cells of widget instead of rows of widget.

class _tableexamplestate extends state<tableexample> {
  list<reorderabletablerow> _itemrows;

  @override
  void initstate() {
    super.initstate();
    var data = [
      ['alex', 'd', 'b+', 'aa', ''],
      ['bob', 'aaaaa+', '', 'b', ''],
      ['cindy', '', 'to be confirmed', '', ''],
      ['duke', 'c-', '', 'failed', ''],
      ['ellenina', 'c', 'b', 'a', 'a'],
      ['floral', '', 'bbb', 'a', 'a'],
    ];

    widget _textwithpadding(string text) {
      return padding(
        padding: edgeinsets.symmetric(vertical: 4),
        child: text(text, textscalefactor: 1.1),
      );
    }

    _itemrows = data.map((row) {
      return reorderabletablerow(
        //a key must be specified for each row
        key: objectkey(row),
        mainaxissize: mainaxissize.max,
        mainaxisalignment: mainaxisalignment.spacebetween,
        children: <widget>[
          _textwithpadding('${row[0]}'),
          _textwithpadding('${row[1]}'),
          _textwithpadding('${row[2]}'),
          _textwithpadding('${row[3]}'),
//          text('${row[4]}'),
        ],
      );
    }).tolist();
  }

  @override
  widget build(buildcontext context) {
    var headerrow = reorderabletablerow(
      mainaxissize: mainaxissize.max,
      mainaxisalignment: mainaxisalignment.spacebetween,
      children: [
        text('name', textscalefactor: 1.5),
        text('math', textscalefactor: 1.5),
        text('science', textscalefactor: 1.5),
        text('physics', textscalefactor: 1.5),
        text('sports', textscalefactor: 1.5)
      ]
    );

    void _onreorder(int oldindex, int newindex) {
      setstate(() {
        reorderabletablerow row = _itemrows.removeat(oldindex);
        _itemrows.insert(newindex, row);
      });
    }

    return reorderabletable(
      header: headerrow,
      children: _itemrows,
      onreorder: _onreorder,
    );
  }
}

in a table, cells in each row are aligned on column basis with cells in other rows,
whereas cells in a row of a list view don’t align with other rows.

reorderabletable demo

reorderablewrap

this widget can also limit the minimum and maximum amount of children in each run, on top of the size-based policy in wrap’s algorithm. see api references for more details.
*since v0.2.5, children of reorderablewrap don’t need to have a key explicitly specified.

class _wrapexamplestate extends state<wrapexample> {
  final double _iconsize = 90;
  list<widget> _tiles;

  @override
  void initstate() {
    super.initstate();
    _tiles = <widget>[
      icon(icons.filter_1, size: _iconsize),
      icon(icons.filter_2, size: _iconsize),
      icon(icons.filter_3, size: _iconsize),
      icon(icons.filter_4, size: _iconsize),
      icon(icons.filter_5, size: _iconsize),
      icon(icons.filter_6, size: _iconsize),
      icon(icons.filter_7, size: _iconsize),
      icon(icons.filter_8, size: _iconsize),
      icon(icons.filter_9, size: _iconsize),
    ];
  }

  @override
  widget build(buildcontext context) {
    void _onreorder(int oldindex, int newindex) {
      setstate(() {
        widget row = _tiles.removeat(oldindex);
        _tiles.insert(newindex, row);
      });
    }

    var wrap = reorderablewrap(
      spacing: 8.0,
      runspacing: 4.0,
      padding: const edgeinsets.all(8),
      children: _tiles,
      onreorder: _onreorder,
       onnoreorder: (int index) {
        //this callback is optional
        debugprint('${datetime.now().tostring().substring(5, 22)} reorder cancelled. index:$index');
      },
      onreorderstarted: (int index) {
        //this callback is optional
        debugprint('${datetime.now().tostring().substring(5, 22)} reorder started: index:$index');
      }
    );

    var column = column(
      crossaxisalignment: crossaxisalignment.start,
      children: <widget>[
        wrap,
        buttonbar(
          alignment: mainaxisalignment.start,
          children: <widget>[
            iconbutton(
              iconsize: 50,
              icon: icon(icons.add_circle),
              color: colors.deeporange,
              padding: const edgeinsets.all(0.0),
              onpressed: () {
                var newtile = icon(icons.filter_9_plus, size: _iconsize);
                setstate(() {
                  _tiles.add(newtile);
                });
              },
            ),
            iconbutton(
              iconsize: 50,
              icon: icon(icons.remove_circle),
              color: colors.teal,
              padding: const edgeinsets.all(0.0),
              onpressed: () {
                setstate(() {
                  _tiles.removeat(0);
                });
              },
            ),
          ],
        ),
      ],
    );

    return singlechildscrollview(
      child: column,
    );

  }
}

reorderablewrap demo

nested reorderablewrap

it is also possible to nest multiple levels of reorderablewrap. see example/lib/nested_wrap_example.dart for complete example code.

class _nestedwrapexamplestate extends state<nestedwrapexample> {
//  list<widget> _tiles;
  color _color;
  color _colorbrighter;

  @override
  void initstate() {
    super.initstate();
    _color = widget.color ?? colors.primaries[widget.depth % colors.primaries.length];
    _colorbrighter = color.lerp(_color, colors.white, 0.6);
  }

  @override
  widget build(buildcontext context) {
    void _onreorder(int oldindex, int newindex) {
      setstate(() {
        widget._tiles.insert(newindex, widget._tiles.removeat(oldindex));
      });
    }

    var wrap = reorderablewrap(
      spacing: 8.0,
      runspacing: 4.0,
      padding: const edgeinsets.all(8),
      children: widget._tiles,
      onreorder: _onreorder
    );

    var buttonbar = container(
      color: _colorbrighter,
      child: row(
        mainaxissize: mainaxissize.min,
        mainaxisalignment: mainaxisalignment.start,
        children: <widget>[
          iconbutton(
            iconsize: 42,
            icon: icon(icons.add_circle),
            color: colors.deeporange,
            padding: const edgeinsets.all(0.0),
            onpressed: () {
              setstate(() {
                widget._tiles.add(
                  card(
                    child: container(
                      child: text('${widget.valueprefix}${widget._tiles.length}', textscalefactor: 3 / math.sqrt(widget.depth + 1)),
                      padding: edgeinsets.all((24.0 / math.sqrt(widget.depth + 1)).roundtodouble()),
                    ),
                    color: _colorbrighter,
                    elevation: 3,
                  )
                );
              });
            },
          ),
          iconbutton(
            iconsize: 42,
            icon: icon(icons.remove_circle),
            color: colors.teal,
            padding: const edgeinsets.all(0.0),
            onpressed: () {
              setstate(() {
                widget._tiles.removeat(0);
              });
            },
          ),
          iconbutton(
            iconsize: 42,
            icon: icon(icons.add_to_photos),
            color: colors.pink,
            padding: const edgeinsets.all(0.0),
            onpressed: () {
              setstate(() {
                widget._tiles.add(nestedwrapexample(depth: widget.depth + 1, valueprefix: '${widget.valueprefix}${widget._tiles.length}.',));
              });
            },
          ),
          text('level ${widget.depth} / ${widget.valueprefix}'),
        ],
      )
    );

    var column = column(
      crossaxisalignment: crossaxisalignment.start,
      children: [
        buttonbar,
        wrap,
      ]
    );

    return singlechildscrollview(
      child: container(child: column, color: _color,),
    );
  }
}

nested reorderablewrap demo

reorderablecolumn example #1

class _columnexample1state extends state<columnexample1> {
  list<widget> _rows;

  @override
  void initstate() {
    super.initstate();
    _rows = list<widget>.generate(50,
        (int index) => text('this is row $index', key: valuekey(index), textscalefactor: 1.5)
    );
  }

  @override
  widget build(buildcontext context) {
    void _onreorder(int oldindex, int newindex) {
      setstate(() {
        widget row = _rows.removeat(oldindex);
        _rows.insert(newindex, row);
      });
    }

    return reorderablecolumn(
      header: text('this is the header row'),
      footer: text('this is the footer row'),
      crossaxisalignment: crossaxisalignment.start,
      children: _rows,
      onreorder: _onreorder,
    );
  }
}

reorderablecolumn example #1 demo

reorderablecolumn example #2

class _columnexample2state extends state<columnexample2> {
  list<widget> _rows;

  @override
  void initstate() {
    super.initstate();
    _rows = list<widget>.generate(10,
        (int index) => text('this is row $index', key: valuekey(index), textscalefactor: 1.5)
    );
  }

  @override
  widget build(buildcontext context) {
    void _onreorder(int oldindex, int newindex) {
      setstate(() {
        widget row = _rows.removeat(oldindex);
        _rows.insert(newindex, row);
      });
    }

    widget reorderablecolumn = intrinsicwidth(
      child: reorderablecolumn(
        header: text('list-like view but supports intrinsicwidth'),
//        crossaxisalignment: crossaxisalignment.start,
        children: _rows,
        onreorder: _onreorder,
      )
    );

    return transform(
      transform: matrix4.rotationz(0),
      alignment: fractionaloffset.topleft,
      child: material(
        child: card(child: reorderablecolumn),
        elevation: 6.0,
        color: colors.transparent,
        borderradius: borderradius.zero,
      ),
    );
  }
}

reorderablecolumn example #2 demo

reorderablerow

see exmaple/lib/row_example.dart

reorderablerow demo


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.