2023-06-21 17:44:28 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:reorderables/reorderables.dart';
|
|
|
|
|
|
|
|
void main() => runApp(MaterialApp(
|
|
|
|
title: 'Reorderable Sliver List Demo',
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-03-31 21:24:02 +08:00
|
|
|
title: const Text('Reorderable Sliver List Demo'),
|
2023-06-21 17:44:28 +08:00
|
|
|
),
|
2024-03-31 21:24:02 +08:00
|
|
|
body: const SliverExample(),
|
2023-06-21 17:44:28 +08:00
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
class SliverExample extends StatefulWidget {
|
2024-03-31 21:24:02 +08:00
|
|
|
const SliverExample({super.key});
|
|
|
|
|
2023-06-21 17:44:28 +08:00
|
|
|
@override
|
|
|
|
_SliverExampleState createState() => _SliverExampleState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SliverExampleState extends State<SliverExample> {
|
|
|
|
late List<Widget> _rows;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_rows = List<Widget>.generate(
|
|
|
|
50,
|
2024-03-31 21:24:02 +08:00
|
|
|
(int index) => SizedBox(
|
2023-06-21 17:44:28 +08:00
|
|
|
width: double.infinity,
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
child: Text('This is sliver child $index', textScaleFactor: 2),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-03-31 21:24:02 +08:00
|
|
|
void onReorder(int oldIndex, int newIndex) {
|
2023-06-21 17:44:28 +08:00
|
|
|
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.
|
2024-03-31 21:24:02 +08:00
|
|
|
ScrollController scrollController =
|
2023-06-21 17:44:28 +08:00
|
|
|
PrimaryScrollController.maybeOf(context) ?? ScrollController();
|
|
|
|
|
|
|
|
return CustomScrollView(
|
|
|
|
// A ScrollController must be included in CustomScrollView, otherwise
|
|
|
|
// ReorderableSliverList wouldn't work
|
2024-03-31 21:24:02 +08:00
|
|
|
controller: scrollController,
|
2023-06-21 17:44:28 +08:00
|
|
|
slivers: <Widget>[
|
|
|
|
SliverAppBar(
|
|
|
|
expandedHeight: 210.0,
|
|
|
|
flexibleSpace: FlexibleSpaceBar(
|
2024-03-31 21:24:02 +08:00
|
|
|
title: const Text('ReorderableSliverList'),
|
2023-06-21 17:44:28 +08:00
|
|
|
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
|
|
|
|
// ),
|
2024-03-31 21:24:02 +08:00
|
|
|
onReorder: onReorder,
|
2023-06-21 17:44:28 +08:00
|
|
|
onNoReorder: (int index) {
|
|
|
|
//this callback is optional
|
|
|
|
debugPrint(
|
|
|
|
'${DateTime.now().toString().substring(5, 22)} reorder cancelled. index:$index',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onReorderStarted: (int index) {
|
|
|
|
debugPrint(
|
|
|
|
'${DateTime.now().toString().substring(5, 22)} reorder started. index:$index',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|