title: Flutter: How do I change theme brightness at runtime?
body: <p>I have a <code>MaterialApp</code> with a <code>ThemeData</code> that is initially set to <code>Brightness.light</code>.  I'd like to switch the brightness to <code>Brightness.dark</code> at runtime, but when I make that change, only the status bar changes - none of the Flutter widgets actually change their brightness.</p>
  
  <p>How can this behavior be achieved?</p>
  
  <p>To change the <code>ThemeData</code> at runtime, I've created the following <code>StatefulWidget</code> that wraps my <code>MaterialApp</code> and rebuilds it whenever the theme changes:</p>
  
  <pre><code>final ThemeData appTheme = ThemeData(
    brightness: Brightness.light,
  );
  
  class ThemeChanger extends StatefulWidget {
    static ThemeChangerState of (BuildContext context) {
      return context.ancestorStateOfType(TypeMatcher&lt;ThemeChangerState&gt;());
    }
  
    ThemeChanger({
      this.childBuilder,
    });
  
    final Widget Function(BuildContext, ThemeData) childBuilder;
  
    @override
    ThemeChangerState createState() =&gt; ThemeChangerState();
  }
  
  class ThemeChangerState extends State&lt;ThemeChanger&gt; {
    Brightness _brightness = Brightness.light;
  
    set brightness(Brightness brightness) {
      setState(() {
        _brightness = brightness;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return widget.childBuilder(
        context,
        appTheme.copyWith(
          brightness: _brightness
        ),
      );
    }
  }
  
  // Then in main.dart
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return DemoTheme(
        childBuilder: (BuildContext context, ThemeData theme) {
          return MaterialApp(
            title: 'Materially Better',
            theme: theme,
            routes: {
              '/': (BuildContext context) {
                return LoginScreen();
              },
              'home': (BuildContext context) {
                return MainScreen();
              }
            },
            debugShowCheckedModeBanner: false,
          );
        },
      );
    }
  }
  </code></pre>