BirjuVachhani / adaptive_theme

Easiest way to add support for light and dark theme in your flutter app.
https://pub.dev/packages/adaptive_theme
Apache License 2.0
464 stars 37 forks source link

unable to change to light mode #30

Closed kyleorin closed 2 years ago

kyleorin commented 2 years ago

happening on iOS haven't tested on other platforms; trying to change the initial value to light or system doesn't work even when MediaQuery reads the theme correctly.

not sure what im doing wrong here

Widget build(BuildContext context) {
    return AdaptiveTheme(
      light: ThemeData.light().copyWith(
          brightness: Brightness.light,
          primaryColor: (Platform.isIOS || Platform.isMacOS)
              ? CupertinoTheme.of(context).primaryColor
              : Theme.of(context).primaryColor,
          dividerColor: CupertinoColors.inactiveGray,
          scaffoldBackgroundColor: CupertinoColors.white,
          cardColor: CupertinoColors.systemGroupedBackground,
          primaryIconTheme:
              Theme.of(context).primaryIconTheme.copyWith(color: Colors.black),
          textTheme: Theme.of(context).textTheme.copyWith(
              bodyText1: Theme.of(context)
                  .textTheme
                  .bodyText1!
                  .copyWith(color: CupertinoColors.black),
              subtitle1: Theme.of(context)
                  .textTheme
                  .subtitle1!
                  .copyWith(color: CupertinoColors.black),
              subtitle2: Theme.of(context).textTheme.subtitle2!.copyWith(
                  color: CupertinoTheme.of(context)
                      .textTheme
                      .tabLabelTextStyle
                      .color),
              headline6: Theme.of(context).textTheme.headline6!.copyWith(
                    fontWeight: CupertinoTheme.of(context)
                        .textTheme
                        .navLargeTitleTextStyle
                        .fontWeight,
                    color: (Platform.isIOS || Platform.isMacOS)
                        ? CupertinoTheme.of(context).textTheme.textStyle.color
                        : Theme.of(context).textTheme.subtitle1!.color,
                  ),
              headline4: Theme.of(context).textTheme.headline4!.copyWith(
                  fontWeight: CupertinoTheme.of(context)
                      .textTheme
                      .navLargeTitleTextStyle
                      .fontWeight,
                  color: (Platform.isIOS || Platform.isMacOS)
                      ? CupertinoColors.white
                      : Theme.of(context).textTheme.subtitle1!.color)),
          appBarTheme: Theme.of(context).appBarTheme.copyWith(
              backgroundColor: CupertinoTheme.of(context).barBackgroundColor,
              titleTextStyle: CupertinoTheme.of(context)
                  .textTheme
                  .navTitleTextStyle
                  .copyWith(color: CupertinoColors.black),
              textTheme: Theme.of(context).textTheme.copyWith(
                  bodyText1: Theme.of(context)
                      .textTheme
                      .bodyText1!
                      .copyWith(color: CupertinoColors.black),
                  subtitle1: Theme.of(context)
                      .textTheme
                      .subtitle1!
                      .copyWith(color: CupertinoColors.black),
                  subtitle2: Theme.of(context).textTheme.subtitle2!.copyWith(
                      color: CupertinoTheme.of(context)
                          .textTheme
                          .tabLabelTextStyle
                          .color),
                  headline6:
                      Theme.of(context).textTheme.headline6!.copyWith(fontWeight: CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle.fontWeight, color: (Platform.isIOS || Platform.isMacOS) ? CupertinoColors.black : Theme.of(context).textTheme.subtitle1!.color)),
              toolbarTextStyle: CupertinoTheme.of(context).textTheme.actionTextStyle)),
      dark: ThemeData.dark().copyWith(
          brightness: Brightness.dark,
          //backgroundColor: Theme.of(context).scaffoldBackgroundColor,
          primaryColor: (Platform.isIOS || Platform.isMacOS)
              ? CupertinoTheme.of(context).primaryColor
              : Theme.of(context).primaryColor,
          dividerColor: CupertinoColors.inactiveGray,
          scaffoldBackgroundColor: CupertinoColors.black,
          cardColor: CupertinoColors.darkBackgroundGray,
          textTheme: Theme.of(context).textTheme.copyWith(
              bodyText1: Theme.of(context)
                  .textTheme
                  .bodyText1!
                  .copyWith(color: CupertinoColors.white),
              subtitle1: Theme.of(context)
                  .textTheme
                  .subtitle1!
                  .copyWith(color: CupertinoColors.white),
              subtitle2: Theme.of(context).textTheme.subtitle2!.copyWith(
                  color: CupertinoTheme.of(context)
                      .textTheme
                      .tabLabelTextStyle
                      .color),
              headline6: Theme.of(context).textTheme.headline6!.copyWith(
                  fontWeight: CupertinoTheme.of(context)
                      .textTheme
                      .navLargeTitleTextStyle
                      .fontWeight,
                  color: (Platform.isIOS || Platform.isMacOS)
                      ? CupertinoColors.white
                      : Theme.of(context).textTheme.subtitle1!.color),
              headline4: Theme.of(context).textTheme.headline4!.copyWith(
                  fontWeight: CupertinoTheme.of(context)
                      .textTheme
                      .navLargeTitleTextStyle
                      .fontWeight,
                  color: (Platform.isIOS || Platform.isMacOS)
                      ? CupertinoColors.white
                      : Theme.of(context).textTheme.subtitle1!.color)),
          appBarTheme: Theme.of(context)
              .appBarTheme
              .copyWith(backgroundColor: CupertinoTheme.of(context).barBackgroundColor, titleTextStyle: CupertinoTheme.of(context).textTheme.navTitleTextStyle.copyWith(color: CupertinoColors.white), textTheme: Theme.of(context).textTheme.copyWith(bodyText1: Theme.of(context).textTheme.bodyText1!.copyWith(color: CupertinoColors.white), subtitle1: Theme.of(context).textTheme.subtitle1!.copyWith(color: CupertinoColors.white), subtitle2: Theme.of(context).textTheme.subtitle2!.copyWith(color: CupertinoTheme.of(context).textTheme.tabLabelTextStyle.color), headline6: Theme.of(context).textTheme.headline6!.copyWith(fontWeight: CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle.fontWeight, color: (Platform.isIOS || Platform.isMacOS) ? CupertinoColors.white : Theme.of(context).textTheme.subtitle1!.color)), toolbarTextStyle: CupertinoTheme.of(context).textTheme.actionTextStyle)),
      initial: AdaptiveThemeMode.dark,
      builder: (theme, darkTheme) => MaterialApp(
        title: 'Flutter',
        theme: theme,
        darkTheme: darkTheme,
        home: LoginScreen(),
      ),
    );
  }
BirjuVachhani commented 2 years ago

Can you specify iOS version?

BirjuVachhani commented 2 years ago

You don't change the initial value to change the theme. Do this instead.

AdaptiveTheme.of(context).setDark()

kyleorin commented 2 years ago

hi @BirjuVachhani sorry for the long wait, iOS is the latest version (currently 15) im looking to set the theme along with the device's theme is there any way to do that without the initial statement?

BirjuVachhani commented 2 years ago

Use this:

AdaptiveTheme.of(context).setSystem();
kyleorin commented 2 years ago

getting this: The named parameter 'initial' is required, but there's no corresponding argument. Try adding the required argument.

reedrosenbluth commented 2 years ago

You don't change the initial value to change the theme. Do this instead.

AdaptiveTheme.of(context).setDark()

Where would you typically put this code?

kyleorin commented 2 years ago

changing the initial to initial: AdaptiveThemeMode.system worked after updating Xcode with the latest simulator version.

@reedrosenbluth I'd put it in a theme settings switch or chooser and go from there.