1 year ago
#342748
mollusk
Refresh an item of ListView
I have a problem to refresh my ListView. I manage to modify my item, but after the pop it does not update, and I have to exit and return to the page to see the modification.
In my UserInfoCard, the background is grey if the user have a deletedAt not null.
When I see my item of list after pressed button OK, my user is always enabled. I exit/return to the page and my user disabled and the background is grey.
class UserListPage extends StatefulWidget {
const UserListPage({Key? key}) : super(key: key);
@override
_UserListPageState createState() => _UserListPageState();
}
class _UserListPageState extends State<UserListPage> {
late UserApi _api;
late User userSelected;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
_api = UserApi();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _api.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) {
List<User> users = snapshot.data!;
return Scaffold(
appBar: MyToolBar(),
body: Column(
children: [
Expanded(child: _buildListView(users)),
],
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
);
}
Widget _buildListView(List<User> list) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: DEFAULT_PADDING),
child: SlidableAutoCloseBehavior(
key: _scaffoldKey,
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
User item = list[index];
return Slidable(
key: const ValueKey(0),
startActionPane: ActionPane(
motion: const ScrollMotion(),
children: _buildSlidableAction(context, item),
),
child: GestureDetector(
onTap: () => Navigator.pushReplacementNamed(
context, Routes.admUserForm,
arguments: {"fenType": FenType.read, "user": item}),
child: UserInfoCard(
pseudo: item.pseudo!,
familyName: item.familyName,
resume: item.resume,
avatar: item.avatar,
deletedAt: item.deletedAt),
),
);
},
itemCount: list.length,
),
));
}
List<SlidableAction> _buildSlidableAction(BuildContext context, User item) {
bool isDesactived = (item.deletedAt != null) ? false : true;
return [
SlidableAction(
onPressed: (context) {
userSelected = item;
_showDialog();
},
backgroundColor:
isDesactived ? Colors.deepOrangeAccent : Colors.lightGreen,
foregroundColor: Colors.white,
icon: isDesactived
? Icons.do_not_disturb_outlined
: Icons.check_circle_outline_outlined),
SlidableAction(
onPressed: (context) => Navigator.pushReplacementNamed(
context, Routes.admUserForm,
arguments: {"fenType": FenType.update, "user": item}),
backgroundColor: Colors.lightBlue,
foregroundColor: Colors.white,
icon: Icons.edit),
];
}
void Function() koButtonPressed() => () {
Navigator.of(_scaffoldKey.currentContext!).pop(false);
};
void Function() okButtonPressed() => () {
setState(() {
late SnackBar snackBar;
bool isDesactived = (userSelected.deletedAt != null) ? false : true;
User userDisabled = userSelected;
userDisabled.deletedAt = isDesactived ? DateTime.now() : null;
_api.disabled(userDisabled).then((value) {
if (value) {
String message = isDesactived ? 'User disabled' : 'User enabled';
snackBar = MyWidget.okSnackBar(message);
userSelected.deletedAt = userDisabled.deletedAt;
} else {
String message =
isDesactived ? 'Don\'t User disabled' : 'Don\'t User enabled';
snackBar = MyWidget.koSnackBar(message);
}
ScaffoldMessenger.of(context).showSnackBar(snackBar);
});
Navigator.of(_scaffoldKey.currentContext!).pop();
});
};
void _showDialog() {
bool enable = (userSelected.deletedAt == null) ? true : false;
String pseudo = userSelected.pseudo!;
String title = enable ? "Disable" : "Enable";
String message = enable ? "Disable $pseudo ?" : "Enable $pseudo ?";
String textKoButton = "Cancel";
String textOkButton = enable ? "Disable" : 'Enable';
MyDialog alert = MyDialog(
title: title,
message: message,
onPressedKo: koButtonPressed(),
onPressedOk: okButtonPressed(),
textKoButton: textKoButton,
textOkButton: textOkButton);
showDialog(
context: _scaffoldKey.currentContext!,
builder: (BuildContext context1) {
return alert;
},
);
}
}
flutter
listviewitem
flutter-listview
0 Answers
Your Answer