How to use showTimePicker as Widget in flutter? - flutter

I want the user to pick the date and time, for that there is date time picker dialogue.
But, is there a way that, I could show the date-time persistently on some flutter widget and use like any other widget?
Container(
child: showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.dark(),
child: child,
);
},
);
}
but I cannot use showTimePicker as Widget.
How to use showTimePicker() as widget? so that other widgets can be built on top of it.

I ran into the problem just like you some time ago and I copied the source code and made my custom widget. Now, it can be used like any widget. If you want to adopt this, I want to mention a couple of things.
I am not sure if this works well with localization, I did not test that.
I am not sure if this works on other themes than the light theme, I only tested on the light theme.
You can find the code here.
https://gist.github.com/mithunadhikari40/b55b9990ebc15d0d8bf70fd3f87709b0
Usage:
Copy the code from the above link, create a dart file, paste the code and use it like this:
SizedBox(
child: TimePickerDialog(
initialTime: TimeOfDay.fromDateTime(DateTime.now()),
onTimeChange: (TimeOfDay time) {
print(
"What we get the value of the time is now $time");
},
),
),

You have to open showTimePicker on click of any widget. So you can simply put your code of showTimePicker inside onTap property of the GestureDetector as shown below.
GestureDetector(
onTap: () async {
TimeOfDay picked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: true),
child: child,
);
},);
},
child: Text("SetTime",textAlign: TextAlign.center,));

Related

I want a similar screen (one on top of other) like following to complete my weather app made using flutter . Can anyone please help me out?

I want this type of screen one on top of other and the previous screen should be lightly blurred or it should slightly darkens
You could use 'ModalBottomSheet' or even 'DraggableScrollableSheet' if a list is associated.
ModalBottomSheet
showModalBottomSheet(
context: context,
builder: (context) {
return Container(
// widgets
),
);
});
DraggableScrollableSheet
DraggableScrollableSheet(
builder:
(BuildContext context, ScrollController scrollController) {
return Container(
// widgets
);
},
),

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

Change AppBar title value on carousel change without setstate

I'm developing a gallery app looking like whatsapps photo gallery. I want the appbar title change when my carousel slider changes. I tried it with setstate but because I show the carouselslider in a showdialog, it doesn't work and I think it can be cause performance issues. I use getx in the project and is there any way to do it without setstate?
Here is my AppBar widget
showGeneralDialog(
context: context,
barrierDismissible: true,
barrierLabel: MaterialLocalizations.of(context)
.modalBarrierDismissLabel,
barrierColor: Colors.black45,
transitionDuration:
const Duration(milliseconds: 200),
pageBuilder: (BuildContext buildContext,
Animation animation,
Animation secondaryAnimation) {
return Dismissible(
direction: DismissDirection.vertical,
key: const Key('key'),
onDismissed: (_) => Navigator.of(context).pop(),
child: Scaffold(
appBar: AppBar(title: Text(photoSender)),
And body of the scaffold is a carouselsliderbuilder, so when the carousel changes, I want the photoSender variable to change.
Any ways to do this?
Declare you photoSender variable as Rx<String>:
final photoSender = Rx<String>("");
Wrap your Text widget with Obx/GetX:
appBar: AppBar(title: Obx(()=>Text(photoSender.value))),
Whenever your photoSender variable`s value changes, it will automatically update the text in your app bar.
To update the value:
photoSender.value = "John";

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