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

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,
);

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)

The change in showModalBottomSheet is fixed by closing-opening

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()
}
)
}
);

Is it possible to use onTap with FirebaseAnimatedList

i'm pretty new in Flutter. So i wanted to ask you guys, is it possible using FirebaseAnimatedList to make it pop another window when user presses the list item. I have a feeling that it not be possible to do it, would be great if someone could explain it.
child: FirebaseAnimatedList(
query: dbRef,
itemBuilder: (BuildContext context, DataSnapshot snapshot,
Animation<double> animation, int index) {
Map contact = snapshot.value;
return _buildContactItem(contact: contact);
},
),
Instance of onTap code snippet
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ItemPage(uid: widget.uid)),
);
},
)
If I understand u well, u want to make onPress method for each listItem?
just wrap your widget with Inkwell inside your itembuilder.
For example:
_buildContactItem(Contact contact){
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ItemPage(uid: widget.uid)),
);
},
child: yourChildWidgetsHere
}
Try this, I hope it will help you!

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 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');},