Skip to content

Instantly share code, notes, and snippets.

@ShaiqAhmedkhan
Created August 21, 2020 09:46
Show Gist options
  • Save ShaiqAhmedkhan/3ce7736b6f40a9de09c545449c5f6597 to your computer and use it in GitHub Desktop.
Save ShaiqAhmedkhan/3ce7736b6f40a9de09c545449c5f6597 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class ReorderableViewPage extends StatefulWidget {
List<String> item = ["Clients","Designer","Developer","Director",
"Employee", "Manager", "Worker","Owner"];
@override
_ReorderableViewPageState createState() => _ReorderableViewPageState();
}
class _ReorderableViewPageState extends State<ReorderableViewPage> {
void reorderData(int oldindex, int newindex){
setState(() {
if(newindex>oldindex){
newindex-=1;
}
final items =widget.item.removeAt(oldindex);
widget.item.insert(newindex, items);
});
}
void sorting(){
setState(() {
widget.item.sort();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[400],
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Reorderable ListView In Flutter",
style: TextStyle(color: Colors.pinkAccent[100]),
),
centerTitle: true,
actions: <Widget>[
IconButton(icon: Icon(Icons.sort_by_alpha),
tooltip:"Sort",
onPressed: sorting
),
],
),
body: ReorderableListView(
children: <Widget>[
for(final items in widget.item)
Card(
color: Colors.blueGrey,
key: ValueKey(items),
elevation: 2,
child: ListTile(
title: Text(items),
leading: Icon(Icons.work,color: Colors.black,),
),
),
],
onReorder: reorderData,
),
);
}
}
@reymillenium
Copy link

reymillenium commented Apr 12, 2021

There is a little mistake in the naming of the variables:

It should be:
for(final item in widget.items)

and not:
for(final items in widget.item)

Because in the declaration of the Stateful widget it should be:

class ReorderableViewPage extends StatefulWidget {
  List<String> items = ["Clients","Designer","Developer","Director",
    "Employee", "Manager", "Worker","Owner"];
  @override
  _ReorderableViewPageState createState() => _ReorderableViewPageState();
}

and not:

class ReorderableViewPage extends StatefulWidget {
  List<String> item = ["Clients","Designer","Developer","Director",
    "Employee", "Manager", "Worker","Owner"];
  @override
  _ReorderableViewPageState createState() => _ReorderableViewPageState();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment