0

I have a widget that returns showModalBottomSheet, which is inside of void. I call it inside of initState class. But when I change values inside of showModalBottomSheet with setState, it is not displayed even though values changes.

initState()

 @override
  void initState() {
    Future.delayed(Duration.zero, () {
      showModal();
    });
    super.initState();
  }

showModalBottomSheet void

  void showModal() {
    showModalBottomSheet<void>(
        isDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return SingleChildScrollView(

I'm changing value

                        child: GestureDetector(
                          child: Container(
                            decoration: BoxDecoration(
                              border: Border.all(color: Colors.black),
                              color: color,
                              borderRadius: BorderRadius.circular(50.0),
                            ),
                            height: MediaQuery.of(context).size.width / 10,
                            width: MediaQuery.of(context).size.width / 10,
                          ),
                          onTap: () => showModalBottomSheet(
                              context: context,
                              builder: (_) {
                                return Container(
                                  height: 300,
                                  child: ColorPickerWidget(this.callbackColor),
                                );
                              }),
                        ),
  void callbackColor(Color color) {
    setState(() {
      this.color = color;
    });
  }
6
  • ModalSheet inside initState(), doesn't sound a good practice, can you explain the use case.? Apr 19, 2021 at 9:34
  • When i press button inside of parent widget i want showModalBottomSheet to show up. But as this widget is big enough, i decided to move it inside of another widget.
    – Nik Evi
    Apr 19, 2021 at 9:47
  • No need to write it in initState. InitState has some other reason to exist. Apr 19, 2021 at 9:48
  • But how to call it without any actions in other way?
    – Nik Evi
    Apr 19, 2021 at 9:53
  • 1
    As you said you have a button in parent class which on pressed will show the sheet. So just call showModalSheet on button Press Apr 19, 2021 at 9:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.