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

Dynamically switching based on macOS not working #23

Closed edwardaux closed 3 years ago

edwardaux commented 3 years ago

For some reason, if I switch the macOS theme from light to dark (or vice versa) in System Settings, my macOS flutter app doesn't dynamically switch.

If I'm running the app from VS Code and manually reload (or do a full restart), then it will use whatever the system is at that time. However, if I switch the operating system setting after starting the app, it doesn't dynamically change.

Is the expected behaviour that the new theme is only re-applied after app restart?

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

Future<void> main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AdaptiveTheme(
        light: ThemeData(brightness: Brightness.light, primarySwatch: Colors.red),
        dark: ThemeData(brightness: Brightness.dark, primarySwatch: Colors.red),
        initial: AdaptiveThemeMode.system,
        builder: (theme, darkTheme) {
          return MaterialApp(theme: theme, darkTheme: darkTheme, home: Scaffold(body: Text('hi there')));
        });
  }
}

Steps to reproduce the behavior:

  1. Create a macOS flutter app
  2. Add adaptive_theme to pubspec
  3. Run the app with the code above
  4. Switch the operating system theme
  5. Observe that the flutter app theme doesn't change dynamically

    • OS: macOS Catalina 10.15.7
    • Adaptive_theme: 2.2.0
BirjuVachhani commented 3 years ago

@edwardaux This doesn't really depend on this package. If you have passed AdaptiveThemeMode.system then it should work! I don't have a macOS system but I'll check the same with Windows.

BirjuVachhani commented 3 years ago

@edwardaux So I checked with Windows and it seems that something's wrong with Flutter SDK itself.

Try this simple example and it doesn't involve using the library and it should based on which theme has been set system wide but it doesn't work on windows. Maybe you could check the same on Mac?

MaterialApp(
  title: 'System Theme Demo',
  theme: ThemeData.light(),
  darkTheme: ThemeData.dark(),
  home: MyHomePage(),
);
BirjuVachhani commented 3 years ago

@edwardaux Check this thread: https://github.com/flutter/flutter/issues/86975#issuecomment-890751189

edwardaux commented 3 years ago

Ah, yes indeed, that's the problem. Running from the command line, the simple example above works just fine (even without adaptive_theme). Looks like it is only a problem when running from VS Code.

I'll close this ticket out then. Thanks for tracking down the problem.

BirjuVachhani commented 3 years ago

Happy to help! ☺️ 👍🏻