Want to make a scrollable wrapping view with Flutter? You can’t easily solve this with an infinite-length ListView.builder because it only goes in one direction. If you want to wrap in both directions, it is possible to simulate bidirectional wrapping with a Stack of two viewports going in opposite directions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import 'package:flutter/material.dart'; void main() { runApp(new MaterialApp( home: new HomePage(), )); } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Wrapping List View'), ), body: new WrappingListView.builder( itemCount: 10, itemBuilder: (BuildContext context, int index) { return new Card( child: new Container( height: 50.0, color: Colors.blue.withOpacity(index / 10), child: new Center( child: new Text('Card $index') ), ), ); }, ), ); } } class WrappingListView extends StatefulWidget { factory WrappingListView({ Key key, List<Widget> children }) { return new WrappingListView.builder( itemCount: children.length, itemBuilder: (BuildContext context, int index) { return children[index % children.length]; }, ); } WrappingListView.builder({ Key key, this.itemBuilder, this.itemCount }) : super(key: key); final int itemCount; final IndexedWidgetBuilder itemBuilder; WrappingListViewState createState() => new WrappingListViewState(); } class UnboundedScrollPosition extends ScrollPositionWithSingleContext { UnboundedScrollPosition({ ScrollPhysics physics, ScrollContext context, ScrollPosition oldPosition, }) : super(physics: physics, context: context, oldPosition: oldPosition); @override double get minScrollExtent => double.negativeInfinity; } class UnboundedScrollController extends ScrollController { @override UnboundedScrollPosition createScrollPosition( ScrollPhysics physics, ScrollContext context, ScrollPosition oldPosition, ) { return new UnboundedScrollPosition( physics: physics, context: context, oldPosition: oldPosition, ); } } class WrappingListViewState extends State<WrappingListView> { UnboundedScrollController _controller = new UnboundedScrollController(); UnboundedScrollController _negativeController = new UnboundedScrollController(); @override void initState() { _controller.addListener(() { _negativeController.jumpTo( -_negativeController.position.extentInside - _controller.position.pixels, ); }); } @override Widget build(BuildContext context) { return new Stack( children: <Widget>[ new CustomScrollView( physics: new AlwaysScrollableScrollPhysics(), controller: _negativeController, reverse: true, slivers: <Widget>[ new SliverList( delegate: new SliverChildBuilderDelegate( (BuildContext context, int index) { return widget.itemBuilder( context, (widget.itemCount - 1 - index) % widget.itemCount, ); } ), ), ], ), new ListView.builder( controller: _controller, itemBuilder: (BuildContext context, int index) { return widget.itemBuilder(context, index % widget.itemCount); }, ), ], ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.