Bonjour,
J’essaye de trier une ListView via une AlertDialog.
Pour cela j’ai une AppBar contenant mon bouton d’action ‹ Sort › qui ouvre mon AlertDialog.
Mon AlertDialog s’ouvre et me laisse le choix pour trier via une liste de bouton Radio.
Ma list _users est bien triée.
Seulement la ListView ne se met pas à jour.
Merci d’avance,
Ma page
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: BAR_TITLE), drawer: const AppDrawer(), body: Scaffold( appBar: _sortBar(), body: SafeArea( child: FutureBuilder( future: apiService.getUsers(), builder: (BuildContext context, AsyncSnapshot<List<User>> snapshot) { if (snapshot.hasError) { return Center( child: Text( "Something wrong with message: ${snapshot.error.toString()}"), ); } else if (snapshot.connectionState == ConnectionState.done) { _users = snapshot.data; return _buildListView(_users!); } else { return const Center( child: CircularProgressIndicator(), ); } }, ), ), )); }
Mon AppBar
PreferredSizeWidget _sortBar() { return AppBar( iconTheme: const IconThemeData(color: Colors.black), titleTextStyle: const TextStyle(color: Colors.black, fontSize: 18), backgroundColor: const Color.fromARGB(255, 228, 228, 228), title: const Text('Les membres !'), actions: <Widget>[ IconButton( icon: const Icon(Icons.filter_list_sharp), onPressed: () { showDialog( context: context, builder: (BuildContext context) => _buildPopupDialog(context), ); }, ), IconButton( icon: const Icon(Icons.search), onPressed: () { setState(() {}); }, ), ], );
}
Mon AlertDialog
Widget _buildPopupDialog(BuildContext context) { return AlertDialog( titlePadding: EdgeInsets.zero, title: Container( height: 45, color: Colors.green, child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text('Trier par'), Container( color: Colors.green, child: GestureDetector( child: const Icon(Icons.clear), onTap: () => Navigator.pop(context), ), ) ])), content: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ ListTile( title: const Text('Nom'), leading: Radio<SortChoose>( value: SortChoose.name, groupValue: _sortChoose, onChanged: (SortChoose? value) { setState(() { _sortChoose = value; }); }, ), ), ListTile( title: const Text('Inscription'), leading: Radio<SortChoose>( value: SortChoose.createdAt, groupValue: _sortChoose, onChanged: (SortChoose? value) { setState(() { _sortChoose = value; }); }, ), ), ], ); }), actionsAlignment: MainAxisAlignment.center, actions: <Widget>[ TextButton( onPressed: () => sorting, child: const Text('Appliquer'), ), ], );
}
void sorting() {
print(‹ sorting $_sortChoose ›);
switch (_sortChoose) {
case SortChoose.name:
_users?.sort((b, a) => a.pseudo!.compareTo(b.pseudo!));
break;
case SortChoose.createdAt:
_users?.sort((a, b) => a.createdAt!.compareTo(b.createdAt!));
break;
default:
_users?.sort((a, b) => a.pseudo!.compareTo(b.pseudo!));
}
print(_users?.first.pseudo);
}