getx icon indicating copy to clipboard operation
getx copied to clipboard

Bottom sheet content not reacting to theme change

Open mdccxv opened this issue 4 years ago • 17 comments

Describe the bug I am using Get.bottomSheet to contain a widget with some app settings, including a toggle for light/dark theme. Get.changeTheme is used for theme change. When the theme is toggled the screen underneath the bottom sheet changes appearance, but the bottom sheet does not.

Reproduction code

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: Center(
          child: RaisedButton(
            child: Text('This shows bottom sheet'),
            onPressed: () {
              Get.bottomSheet(BottomSheetContent());
            },
          ),
        ),
      ),
    );
  }
}

class BottomSheetContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).scaffoldBackgroundColor,
      height: 300,
      child: Center(
        child: RaisedButton(
          child: Text('This toggles the theme'),
          onPressed: () => Get.changeTheme(
            Get.isDarkMode ? ThemeData.light() : ThemeData.dark(),
          ),
        ),
      ),
    );
  }
}

To Reproduce

  1. Click 'This shows bottom sheet', bottom sheet will appear.
  2. Click on 'This toggles the theme'
  3. Observe the theme changing in the background view, but not on the bottom sheet itself

Expected behavior Theme change should apply to the content of bottom sheet

Flutter Version: 1.22.6

Getx Version: 3.24.0

Describe on which device you found the bug: iPhone 12 Pro Max

mdccxv avatar Feb 17 '21 13:02 mdccxv

I second this. Patiently waiting for a fix

oliverbytes avatar Apr 13 '21 03:04 oliverbytes

Same here, any solution available?

JonathanRhein avatar Jul 19 '21 14:07 JonathanRhein

@JonathanRhein I ended up using just a regular showModalBottomSheet instead of Get.bottomSheet. Didn't find a solution though

mdccxv avatar Jul 19 '21 15:07 mdccxv

@mdccxv Thank you! Yes, I will have to do the same which is a bit unfortunate... let's hope there comes a fix soon :-) How do you make the modalBottomSheet reactive to theme changes without using setState() or a StatefulBuilder in this case? showModalBottomSheet cannot be wrapped with GetBuilder in order to be updated on update()...

JonathanRhein avatar Jul 19 '21 17:07 JonathanRhein

@JonathanRhein Get.changeThemeMode works well provided you have set up theme and darkTheme in your GetMaterialApp. It works globally on all the widgets in your app, including modalBottomSheet you're calling the change from.

mdccxv avatar Jul 19 '21 17:07 mdccxv

That is what I did and all other widgets are updated accordingly when I call Get.changeThemeMode, but for some reason it does not change the backgroundColor property of showModalBottomSheet. So I specified a Container in the builder of the showModalBottomSheet to contain all rest of my bottomSheet and made this Container's color property the theme dependent color (using Get.context.theme.scaffoldBackgroundColor) and it works now. Don't understand though why using backgroundColor of showModalBottomSheet does not work... but thank you for your help @mdccxv!!

JonathanRhein avatar Jul 19 '21 20:07 JonathanRhein

In this example I am checking if Dark mode is available and setting Container background color accordingly. In next example I just used container inside Bottom sheet without any additional theme configuration (second screencast).

Get.bottomSheet(
  Container(
    child: Wrap(
      children: [
        ListTile(
          leading: const Icon(Icons.wb_sunny_outlined),
          title: Text('Light Theme'),
          onTap: () => {Get.changeTheme(ThemeData.light())},
        ),
        ListTile(
          leading: const Icon(Icons.wb_sunny),
          title: Text('Dark Theme'),
          onTap: () => {Get.changeTheme(ThemeData.dark())},
        )
      ],
    ),
  ),
  backgroundColor:
      Get.isDarkMode ? Colors.black : Colors.white);
},

Result: Bottom sheet issue

Bottom sheet issue 2

przbadu avatar Oct 23 '21 03:10 przbadu

any updates on this issue??

Faiz-rhm avatar Mar 09 '23 10:03 Faiz-rhm

Also having the same issue with this setup... haven't found a fix if anyone has one please share!

Ugikie avatar Aug 22 '23 17:08 Ugikie

any updates for this issue ?

Souhaibbek avatar Oct 05 '23 13:10 Souhaibbek

Any updates on this issue ?

Zheer03 avatar Jun 22 '24 19:06 Zheer03

Any updates on this issue?

tom-xy avatar Jul 01 '24 08:07 tom-xy

My solution: Wrap the Get.bottomSheet() function

// [Bottom sheet content not reacting to theme change](https://github.com/jonataslaw/getx/issues/1084)
Future<T?> showAppBottomSheet<T>(
  Widget bottomsheet, {
  RouteSettings? settings,
}) {
  return Get.bottomSheet<T>(
    Builder(builder: (context) {
      final ThemeData data;
      switch (MediaQuery.of(context).platformBrightness) {
        case Brightness.dark:
          data = darkTheme();
          break;
        case Brightness.light:
          data = lightTheme();
          break;
      }
      return Theme(data: data, child: bottomsheet);
    }),
    backgroundColor: Colors.transparent,
    isScrollControlled: true,
    settings: settings,
  );
}

tom-xy avatar Jul 01 '24 09:07 tom-xy

I am also facing the same issue with using Get.bottomSheet. The above solution isn't effective when using a bottom navigation bar. Could you please provide an alternative solution?

saprahits avatar Jul 11 '24 12:07 saprahits

Any solution provided yet ?

kriem2000 avatar Oct 12 '24 06:10 kriem2000

I just wrapped my widget inside the Get.bottomSheet with the StatefulBuilder provided by flutter and uses the setModalState only for that scope solved the problem...

Get.bottomSheet(
      StatefulBuilder(
        builder: (BuildContext context, StateSetter setModalState) {
          return Container(
            child: IconButton(
              onPressed: () {
                // your bottomSheet reactive logic
                setModalState(() {
                  // ...
                });
                // your parent widget reactive logic
                setState(() {
                  // ...
                });
              },
              icon: const Icon(Icons.abc),
            ),
          );
        },
      ),
    );

kriem2000 avatar Oct 12 '24 06:10 kriem2000

I am still facing the same issue at this time. This is truly frustrating, though there are multiple ways of getting around it; the time it takes to just understand that the issue stems from Get's is indeed wasted.

Hope we can get an update on this matter

AmirAshvins avatar Apr 10 '25 20:04 AmirAshvins