溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Flutter 拖拽排序組件 ReorderableListView

發(fā)布時間:2020-04-05 09:48:32 來源:網(wǎng)絡(luò) 閱讀:503 作者:mengqingdon 欄目:移動開發(fā)

Flutter 拖拽排序組件 ReorderableListView

注意:無特殊說明,F(xiàn)lutter版本及Dart版本如下:

  • Flutter版本: 1.12.13+hotfix.5
  • Dart版本: 2.7.0

ReorderableListView是通過長按拖動某一項到另一個位置來重新排序的列表組件。

ReorderableListView需要設(shè)置childrenonReorder屬性,children是子控件,onReorder是拖動完成后的回調(diào),用法如下:

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的每個子控件必須設(shè)置唯一的key,ReorderableListView沒有“懶加載”模式,需要一次構(gòu)建所有的子組件,所以ReorderableListView并不適合加載大量數(shù)據(jù)的列表,它適用于有限集合且需要排序的情況,比如手機(jī)系統(tǒng)里面設(shè)置語言的功能,通過拖動對語言排序。

onReorder是拖動完成的回調(diào),第一個參數(shù)是舊的數(shù)據(jù)索引,第二個參數(shù)是拖動到位置的索引,回調(diào)里面需要對數(shù)據(jù)進(jìn)行排序并通過setState刷新數(shù)據(jù)。

效果如下:

Flutter 拖拽排序組件 ReorderableListView

header參數(shù)顯示在列表的頂部,用法如下:

ReorderableListView(
  header: Text(
    '一枚有態(tài)度的程序員',
    style: TextStyle(color: Colors.red,fontSize: 20),
  )
  ...
)

效果如下:

Flutter 拖拽排序組件 ReorderableListView

reverse`參數(shù)設(shè)置為true且ReorderableListView的滾動方向為垂直時,滾動條直接滑動到底部,如果是水平方向則滾動條直接滑動到右邊,默認(rèn)為false,用法如下:

ReorderableListView(
  reverse: true,
  ...
)

scrollDirection`參數(shù)表示滾動到方向,默認(rèn)為垂直,設(shè)置為水平方向如下:

ReorderableListView(
  scrollDirection: Axis.horizontal,
  ...
)

由于改為水平滾動,所以子控件的寬度要設(shè)置,否則會出現(xiàn)沒有列表。

效果如下:

Flutter 拖拽排序組件 ReorderableListView

今天的文章對大家是否有幫助?如果有,請在文章底部留言和點贊,以表示對我的支持,你們的留言、點贊和轉(zhuǎn)發(fā)關(guān)注是我持續(xù)更新的動力!

Flutter 拖拽排序組件 ReorderableListView

更多相關(guān)閱讀:

  • Flutter系列文章總覽
  • 全網(wǎng)最詳細(xì)的一篇Flutter 尺寸限制類容器總結(jié)
  • Flutter DataTable 看這一篇就夠了
  • Flutter Widgets 之 PageView
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI