可以进行长按排序的ListView,可指定滑动方向、是否反向、滑动控制器等属性。

相关组件

ListView

ReorderableListView基本使用

  1. <br />【children】 : 子组件列表 【List<Widget><br />【header】 : 头部组件 【Widget】<br />【padding】 : 内边距 【EdgeInsets】<br />【onReorder】 : 调换时回调 【ReorderCallback】<br />![115.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589454543524-29406ac4-c151-4b92-8b91-25739d275d85.gif#align=left&display=inline&height=518&margin=%5Bobject%20Object%5D&name=115.gif&originHeight=518&originWidth=774&size=1248059&status=done&style=none&width=774)
  1. import 'package:flutter/material.dart';
  2. class CustomReorderableListView extends StatefulWidget {
  3. @override
  4. _CustomReorderableListViewState createState() => _CustomReorderableListViewState();
  5. }
  6. class _CustomReorderableListViewState extends State<CustomReorderableListView> {
  7. var data = <Color>[
  8. Colors.yellow[50],
  9. Colors.yellow[100],
  10. Colors.yellow[200],
  11. Colors.yellow[300],
  12. Colors.yellow[400],
  13. Colors.yellow[500],
  14. Colors.yellow[600],
  15. Colors.yellow[700],
  16. Colors.yellow[800],
  17. Colors.yellow[900],
  18. ];
  19. @override
  20. Widget build(BuildContext context) {
  21. return Container(
  22. height: 250,
  23. child: ReorderableListView(
  24. padding: EdgeInsets.all(10),
  25. header: Container(
  26. color: Colors.blue,
  27. alignment: Alignment.center,
  28. height: 50,
  29. child: Text('长按拖拽进行换位',style: TextStyle(color: Colors.white),)),
  30. onReorder: _handleReorder,
  31. children: data.map((color) => _buildItem(color)).toList(),
  32. ),
  33. );
  34. }
  35. void _handleReorder(int oldIndex, int newIndex) {
  36. if (oldIndex < newIndex) {
  37. newIndex -= 1;
  38. }
  39. setState(() {
  40. final element = data.removeAt(oldIndex);
  41. data.insert(newIndex, element);
  42. });
  43. }
  44. Widget _buildItem(Color color) {
  45. return Container(
  46. key: ValueKey(color) ,
  47. alignment: Alignment.center,
  48. height: 50,
  49. color: color,
  50. child: Text(
  51. colorString(color),
  52. style: TextStyle(color: Colors.white, shadows: [
  53. Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
  54. ]),
  55. ),
  56. );
  57. }
  58. String colorString(Color color) =>
  59. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
  60. }

ReorderableListView滑动方向

  1. <br />【scrollDirection】 : 滑动方向 【Axis】<br />【reverse】 : 是否反向 【bool】<br />![116.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589454614591-0e74a27f-a207-4aba-8409-8e491777fcd7.gif#align=left&display=inline&height=444&margin=%5Bobject%20Object%5D&name=116.gif&originHeight=444&originWidth=810&size=1185852&status=done&style=none&width=810)
  1. import 'package:flutter/material.dart';
  2. class DirectionReorderableListView extends StatefulWidget {
  3. @override
  4. _DirectionReorderableListViewState createState() => _DirectionReorderableListViewState();
  5. }
  6. class _DirectionReorderableListViewState extends State<DirectionReorderableListView> {
  7. var data = <Color>[
  8. Colors.yellow[50],
  9. Colors.yellow[100],
  10. Colors.yellow[200],
  11. Colors.yellow[300],
  12. Colors.yellow[400],
  13. Colors.yellow[500],
  14. Colors.yellow[600],
  15. Colors.yellow[700],
  16. Colors.yellow[800],
  17. Colors.yellow[900],
  18. ];
  19. @override
  20. Widget build(BuildContext context) {
  21. return Container(
  22. height: 200,
  23. child: ReorderableListView(
  24. scrollDirection: Axis.horizontal,
  25. reverse: false,
  26. onReorder: _handleReorder,
  27. children: data.map((color) => _buildItem(color)).toList(),
  28. ),
  29. );
  30. }
  31. void _handleReorder(int oldIndex, int newIndex) {
  32. if (oldIndex < newIndex) {
  33. newIndex -= 1;
  34. }
  35. setState(() {
  36. final element = data.removeAt(oldIndex);
  37. data.insert(newIndex, element);
  38. });
  39. }
  40. Widget _buildItem(Color color) {
  41. return Container(
  42. key: ValueKey(color) ,
  43. alignment: Alignment.center,
  44. width: 80,
  45. color: color,
  46. child: Text(
  47. colorString(color),
  48. style: TextStyle(color: Colors.white, shadows: [
  49. Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
  50. ]),
  51. ),
  52. );
  53. }
  54. String colorString(Color color) =>
  55. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
  56. }