Created
April 10, 2024 17:56
-
-
Save callmephil/9c9ae74d88dc8cf4b3b0cb8f5808d442 to your computer and use it in GitHub Desktop.
Reorderable List Column Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Reorderable ListView Example', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: ReorderableListViewExample(), | |
); | |
} | |
} | |
class ReorderableListViewExample extends StatefulWidget { | |
@override | |
_ReorderableListViewExampleState createState() => | |
_ReorderableListViewExampleState(); | |
} | |
class _ReorderableListViewExampleState | |
extends State<ReorderableListViewExample> { | |
List<String> items = List.generate(10, (index) => "Item $index"); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Reorderable ListView Example'), | |
), | |
body: Column( | |
children: [ | |
Row( | |
children: <Widget>[ | |
Expanded( | |
child: Row( | |
textBaseline: TextBaseline.alphabetic, | |
crossAxisAlignment: CrossAxisAlignment.baseline, | |
children: <Widget>[ | |
Text('Demo '), | |
SizedBox(width: 2), | |
Text('Demo '), | |
], | |
), | |
), | |
Icon(Icons.keyboard_arrow_down), | |
], | |
), | |
SizedBox(height: 8), | |
Expanded( | |
child: ReorderableListView( | |
onReorder: (oldIndex, newIndex) { | |
setState(() { | |
if (oldIndex < newIndex) { | |
newIndex -= 1; | |
} | |
final String item = items.removeAt(oldIndex); | |
items.insert(newIndex, item); | |
}); | |
}, | |
children: <Widget>[ | |
for (final item in items) | |
ListTile( | |
key: ValueKey(item), | |
title: Text(item), | |
), | |
], | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment