I am new in Flutter, I was creating an android app and facing some strange situation where snackbar displays when update method is called but not displaying on create method. Both (update & create) are in one method separated by if else condition. I have left out some code for brevity. Here is the code:
class AddEditCategory extends StatefulWidget {
final BuildContext scaffoldContext;
AddEditCategory({
Key key,
@required this.scaffoldContext,
}) : super(key: key);
@override
_AddEditCategoryState createState() => _AddEditCategoryState();
}
class _AddEditCategoryState extends State<AddEditCategory> {
@override
Widget build(BuildContext context) {
return AlertDialog(
insetPadding: EdgeInsets.symmetric(horizontal: 0),
title: Text(
widget.title,
textAlign: TextAlign.center,
),
content: CategoryTextField(
controller: textEditingController),
actions: [
OutlineButtonAddNewCategory(
positiveAction: widget.positiveAction,
validateDialog: _validateDialog,
textEditingController: textEditingController,
categoryToEdit: widget.categoryToEdit,
scaffoldContext: widget.scaffoldContext,
),
OutlineButton(
child: Text(
widget.negativeAction,
style: TextStyle(color: Colors.redAccent),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
}
}
class OutlineButtonAddNewCategory extends StatelessWidget {
final String positiveAction;
final bool validateDialog;
final TextEditingController textEditingController;
final Category categoryToEdit;
final BuildContext scaffoldContext;
OutlineButtonAddNewCategory(
{this.positiveAction,
this.validateDialog,
this.textEditingController,
this.categoryToEdit,
this.scaffoldContext});
@override
Widget build(BuildContext context) {
return OutlineButton(
child: Text(
positiveAction,
style: TextStyle(
color: validateDialog ? Colors.blueAccent : Colors.grey,
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
onPressed: () async {
if (validateDialog) {
await _submitForm(scaffoldContext,
textEditingController.text.toString(), categoryToEdit);
Navigator.of(context).pop();
}
},
);
}
}
Future _submitForm(BuildContext scaffoldContext, String categoryName,
Category categoryToEdit) async {
CategoryViewModel categoryModel = locator<CategoryViewModel>();
Category category;
if (categoryToEdit != null) {
category = categoryToEdit;
category.id = categoryToEdit.id;
category.categoryName = categoryName;
category.updatedOn = DateTime.now().toIso8601String();
String message = "Category '$categoryName' updated successfully";
await categoryModel.updateCategory(category).then((value) => {
Scaffold.of(scaffoldContext).showSnackBar(
_displaySnackBar(scaffoldContext, message),
)
});
} else {
category = Category(categoryName: categoryName);
String message = "Category '$categoryName' saved successfully";
await categoryModel.createCategory(category).then((value) => {
Scaffold.of(scaffoldContext).showSnackBar(
_displaySnackBar(scaffoldContext, message),
),
});
}
}
Widget _displaySnackBar(BuildContext context, String message) {
return SnackBar(
content: Text(message),
action: SnackBarAction(
label: "Close",
onPressed: () {
Scaffold.of(context).hideCurrentSnackBar();
},
),
);
}
Both update and create are in _submitForm() method. Scaffold context is passed as argument from HomeView(stateless class) page, having Scaffold widget, to CategoryView(stateless class) page and finally in AddEditCategory(stateful) page. For building list of categories I am using Provider with consumer. Viewmodel has been extended with ChangeNotifier class and each method has notifyListeners(). If needed more code than I can provide.
I myself solved the problem of snackbar. Was passing wrong context in the nested widgets and also used Builder to get the Scaffold context for AppBar actions widget list where IconButton was placed to add category.