简体   繁体   中英

How to update the ParentWidget of ModalBottomSheet using setState() in Flutter?

In my SingleDayCalendarView() I have a button which calls a showModalBottomSheet() with AddShiftBottomSheet as its child :

class SingleDayCalendarView extends StatelessWidget {
...
    onPressed: () {
                  showModalBottomSheet(
                      context: context,
                      isScrollControlled: true,
                      enableDrag: true,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(25),
                          topRight: Radius.circular(25),
                        ),
                      ),
                      builder: (BuildContext context) {
                        return AddShiftBottomSheet(
                          dayOfTheShift: id,
                        );
                      });
                },

Then inside AddshiftBottomSheet, which is a stateful widget in another file, I call another showModalBottomSheet to show a TimePicker

class AddShiftBottomSheet extends StatefulWidget {

 @override
  Widget build(BuildContext context) {

DateTime _startDateTime;

...

Text("${DateFormat("HH:mm").format(_startDateTime)}",
    
showModalBottomSheet(
                              
                         context: context,
                              builder: (BuildContext context) {
                                return Container(
                                  height: 200,
                                  color: Colors.white,
                                  child: CupertinoDatePicker(
                                    onDateTimeChanged: (dateTime) {
                                      setState(() {
                                        _startDateTime = dateTime;
                                      });

                                      print(_startDateTime);
                                    },

The problem is that when I change the time with the TimePicker, the Text() which should display the _startDateTime, doesn't change and keeps displaying its initial value. With print statement I see that the variable _startDateTime it's changing as it should and that setState its triggered, but nothing happens.

One strange behavior: if I but the _startDateTime variable between:

class AddShiftBottomSheet extends StatefulWidget {
 @override
  _AddShiftBottomSheetState createState() => _AddShiftBottomSheetState();
}

//here
DateTime = _startDateTime;

class _AddShiftBottomSheetState extends State<AddShiftBottomSheet> {

everything works.

在此处输入图片说明

Remove DateTime _startDateTime; from build method and define it in class scope:

class AddShiftBottomSheet extends StatefulWidget {

DateTime _startDateTime;

 @override
  Widget build(BuildContext context) {
...

When you call setState build method is called again, where you've defined _startDateTime .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM