The change in showModalBottomSheet is fixed by closing-opening - flutter

I have a code. I want to make changes in this code based on whether the variable is true or false. The change happens technically, but in the view, it happens when you close-open.
Codes:
InkWell(
child: Icon(Icons.check_circle, size: 30, color: importantSelected ? Colors.green : Colors.grey,),
onTap: () {
setState(() {
importantSelected = !importantSelected;
});
},
),
These codes are in showModalBottomSheet. The change is visible when I close/open the showModalBottomSheet. I want it to appear directly without turning it on and off.

you have to add StatefulBuilder and you use the new function setState
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return SingleChildScrollView()
}
)
}
);

Related

Is there a way to dim the background while opening the overlay in Flutter?

On click of a button, I am opening an overlay over it. Is there a way to blur or dim the page over which overlay is opened?
Thank you for your time!
Widget build(BuildContext context) {
showDialog(
context: context,
barrierDismissible: true,
barrierLabel:
MaterialLocalizations.of(context).modalBarrierDismissLabel,
barrierColor: Colors.black54.withOpacity(0.8),
builder: (context) {
return AlertDialog(
content: SizedBox(
width: 300,
child: ...
);
});
}
Adjust the opacity to your liking:
Colors.black54.withOpacity(0.8)

How to pop CupertinoPageRoute with swipe down?

Currently I'm using the following Code to get a FadeInTransition from the bottom when opening the page.
Navigator.of(context).push(
CupertinoPageRoute(
fullscreenDialog: true,
maintainState: true,
builder: (BuildContext context) => Routines(),
)
)
Normally in iOS you can just swipe left to close the current page. Is there an easy way to swipe down to pop the current page or do I have to implement a custom field with a GestureDetector()?
The goal is to have a closing transition like in the GIF below using a swipe down at the top
I considered using a DraggableScrollableSheet but I don't want to slide the Page into view from the bottom.
I ended up solving it with an DraggableScrollableSheet:
onPressed: () {
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
isScrollControlled: true,
builder: (BuildContext context) {
return Routines();
},
);
},
and Wrapping my new Page with:
return DraggableScrollableSheet( //TODO make same for Entries
expand: false,
initialChildSize: 1.0,
minChildSize: 0.8,
builder: (context, scrollController) {
return Page2();
}
);

How to close Flutter dialog from outside

This is how we close a dialog from INSIDE
showSomeDialog() {
return showDialog(
context: context,
builder: (BuildContext contextPopup) {
return AlertDialog(
content: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(contextPopup).pop();
},
child: Text('Close me inside'),
),
),);
}
);
}
How to close this dialog from OUTSIDE ? In other words, how does the stateful widget creating this dialog can access the variable contextPopup ? The problem with Navigator.of(context).pop() is that it will close the topmost widget on the Navigation stack, not this specific dialog.
In Android, to close a dialog programmatically from outside, you just do:
dialog.dismiss();

How to pass context to a child widget in flutter

I have a statefull widget W1 which calls a stateless widget W2.
W2 has onTap functionality. I want to show an alert dialog in W2's onTap().
onTap:() {Alert(context: context, title:'Hi');},
I dont get any error, but no alert is shown on tap. I tried passing context as a parameter to W2 but I still dont see any dialog box.
What is the right way to show a dialog box from W2?
I am using rflutter_alert package Link
Thanks
You have to wrap your Alert(context: context, title:'Hi'); with showDialog(context: context, builder: (BuildContext context) => Alert(context: context, title:'Hi'));
Here is the cookbook sample:
Future<void> _neverSatisfied() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Anyway, for your question about how to pass context, If you are creating a Stateless or Stateful widget you dont need to pass the context, you can get it from build(BuildContext context) {}.
Adding .show() in end solved it.
onTap:() {Alert(context: context, title:'Hi').show();}
Its clearly documented in rflutter_alert package, but I somehow missed it.
Pass the context in the function call in onTap:
onTap:(context) {Alert(context: context, title:'Hi');},

how to prevent flutter showBottomSheet from being dismissed by dragging down?

I am using showBottomSheet in flutter to show a persistent bottom sheet. how can I prevent flutter showBottomSheet from being dismissed by dragging down?
I have added my code below. you can place a rawmaterialbutton and with onpressed call this function.
void itemChooser(
{int currentItemCount, String name, callBack, BuildContext context}) {
int chosen = 0;
showBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 500,
color: Colors.white,
);
});
}
Just wrap your child with GestureDetector and set onVerticalDragStart: (_) {},
showBottomSheet(
context: context,
builder: (context) => GestureDetector(
child: *your_widget*,
onVerticalDragStart: (_) {},
),
);
Set enableDrag property of BottomSheet to false its true by default
BottomSheet(
enableDrag: false,
builder: //builder
),
Refer here for more info on BottomSheet
If you use showModalBottomSheet, simply use enableDrag property:
showModalBottomSheet(
context: context,
builder: (context) => yourWidget,
enableDrag: false,
);