For some reason i am not able to remove the current snackbar even though i am calling ScaffoldState().removeCurrentSnackBar()
I am not getting any error messages, but for some reason the snackbar wont go away. Any other way to remove the snackbar when the user picks an item from the DropdownButton?
I have already tried ScaffoldState().hideCurrentSnackBar() and all the other funncntions which are supposed to remove the SnackBar.
Thank you for your answer.
class MainRoute extends StatefulWidget {
@override
_MainRouteState createState() => _MainRouteState();
}
class _MainRouteState extends State<MainRoute> {
List<Currency> dropdownItems = [
Currency(currencyName: "FOO", currencyInUSD: 22.0),
Currency(currencyName: "BOO", currencyInUSD: 22.0),
Currency(currencyName: "SOO", currencyInUSD: 22.0),
];
Currency dropdownValue;
Color color = Colors.green;
MainModel model = MainModel();
@override
void initState() {
super.initState();
dropdownValue = dropdownItems[0];
}
@override
Widget build(BuildContext context) {
return Scaffold(
///A Builder has been added so the context contains the Scaffold which
///is required to create a SnackBar
floatingActionButton: Builder(
builder: (context) {
return FloatingActionButton(
child: Icon(
Icons.add,
color: kIconColor,
),
onPressed: () {
print(dropdownItems.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item.currencyName),
);
}).toList());
setState(() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Which currency do you want to add to your list?'),
SizedBox(
height: 25.0,
),
DropdownButton<Currency>(
items: dropdownItems
.map<DropdownMenuItem<Currency>>((item) {
return DropdownMenuItem(
value: item,
child: Text(item.currencyName),
);
}).toList(),
value: dropdownValue,
onChanged: (value) {
setState(() {
dropdownValue = value;
ScaffoldState().removeCurrentSnackBar(
reason: SnackBarClosedReason.remove);
print("SnackBar removed");
});
},
)
],
),
action: SnackBarAction(
label: 'Cancel',
onPressed: () {
print('Cancel');
},
),
),
);
});
},
);
},
),
);
}
}