注意:無特殊說明,Flutter版本及Dart版本以下:程序員
- Flutter版本: 1.12.13+hotfix.5
- Dart版本: 2.7.0
ReorderableListView是經過長按拖動某一項到另外一個位置來從新排序的列表組件。ide
ReorderableListView須要設置children
和onReorder
屬性,children
是子控件,onReorder
是拖動完成後的回調,用法以下:.net
List<String> items = List.generate(20, (int i) => '$i'); ReorderableListView( children: <Widget>[ for (String item in items) Container( key: ValueKey(item), height: 100, margin: EdgeInsets.symmetric(horizontal: 50, vertical: 10), decoration: BoxDecoration( color: Colors.primaries[int.parse(item) % Colors.primaries.length], borderRadius: BorderRadius.circular(10)), ) ], onReorder: (int oldIndex, int newIndex) { if (oldIndex < newIndex) { newIndex -= 1; } var child = items.removeAt(oldIndex); items.insert(newIndex, child); setState(() {}); }, )
ReorderableListView的每一個子控件必須設置惟一的key,ReorderableListView沒有「懶加載」模式,須要一次構建全部的子組件,因此ReorderableListView並不適合加載大量數據的列表,它適用於有限集合且須要排序的狀況,好比手機系統裏面設置語言的功能,經過拖動對語言排序。code
onReorder
是拖動完成的回調,第一個參數是舊的數據索引,第二個參數是拖動到位置的索引,回調裏面須要對數據進行排序並經過setState
刷新數據。blog
效果以下:排序
header
參數顯示在列表的頂部,用法以下:索引
ReorderableListView( header: Text( '一枚有態度的程序員', style: TextStyle(color: Colors.red,fontSize: 20), ) ... )
效果以下:ci
reverse`參數設置爲true且ReorderableListView的滾動方向爲垂直時,滾動條直接滑動到底部,若是是水平方向則滾動條直接滑動到右邊,默認爲false,用法以下:rem
ReorderableListView( reverse: true, ... )
scrollDirection`參數表示滾動到方向,默認爲垂直,設置爲水平方向以下:get
ReorderableListView( scrollDirection: Axis.horizontal, ... )
因爲改成水平滾動,因此子控件的寬度要設置,不然會出現沒有列表。
效果以下:
今天的文章對你們是否有幫助?若是有,請在文章底部留言和點贊,以表示對個人支持,大家的留言、點贊和轉發關注是我持續更新的動力!