Skip to content

Instantly share code, notes, and snippets.

@VB10
Created December 19, 2021 16:11
Show Gist options
  • Select an option

  • Save VB10/87f00c02d188b0ca1cf51b7bed895b44 to your computer and use it in GitHub Desktop.

Select an option

Save VB10/87f00c02d188b0ca1cf51b7bed895b44 to your computer and use it in GitHub Desktop.
class User {
final String firstName;
User(this.firstName);
static List<User> dummyUsers() {
return [
User('veli'),
User('bora'),
];
}
}
class SortingView extends StatefulWidget {
const SortingView({Key? key}) : super(key: key);
@override
State<SortingView> createState() => _SortingViewState();
}
class _SortingViewState extends State<SortingView> {
final List<User> _users = User.dummyUsers();
bool _isAcending = true;
void _sortByUsers(bool isAscending) {
setState(() {
_users.sort((first, second) => compareAsciiUpperCase(
isAscending ? first.firstName : second.firstName, isAscending ? second.firstName : first.firstName));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.change_circle),
onPressed: () {
_changeAndSort();
},
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
itemBuilder: (context, index) => Card(child: Text(_users[index].firstName)),
itemCount: _users.length,
),
),
);
}
void _changeAndSort() {
setState(() {
_isAcending = !_isAcending;
});
_sortByUsers(_isAcending);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment