kherel / animated_theme_switcher

Flutter animated theme switcher
https://pub.dev/packages/animated_theme_switcher
MIT License
278 stars 28 forks source link

Stateful widgets under `ThemeSwitchingArea` get rebuilt from scratch on changing themes #71

Closed NamanShergill closed 9 months ago

NamanShergill commented 9 months ago

Can be seen by modifying the example to move the counter into a stateful widget. Changing the theme calls the initState on the widget again, and the counter is also back to 0. Specifying a ValueKey doesn't prevent the rebuild either.

import 'package:animated_theme_switcher/animated_theme_switcher.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;

import 'theme_config.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final isPlatformDark =
        WidgetsBinding.instance.window.platformBrightness == Brightness.dark;
    final initTheme = isPlatformDark ? darkTheme : lightTheme;
    return ThemeProvider(
      initTheme: initTheme,
      builder: (_, myTheme) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: myTheme,
          home: const MyHomePage(),
        );
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return ThemeSwitchingArea(
      child: const _Test(key: ValueKey('Temp key')),
    );
  }
}

class _Test extends StatefulWidget {
  const _Test({super.key});

  @override
  State<_Test> createState() => _TestState();
}

class _TestState extends State<_Test> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: SafeArea(
          child: Stack(
            children: <Widget>[
              Align(
                alignment: Alignment.topRight,
                child: ThemeSwitcher.withTheme(
                  builder: (_, switcher, theme) {
                    return IconButton(
                      onPressed: () => switcher.changeTheme(
                        theme: theme.brightness == Brightness.light
                            ? darkTheme
                            : lightTheme,
                      ),
                      icon: const Icon(Icons.brightness_3, size: 25),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
      appBar: AppBar(
        title: const Text(
          'Flutter Demo Home Page',
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: const TextStyle(fontSize: 200),
            ),
            CheckboxListTile(
              title: const Text('Slow Animation'),
              value: timeDilation == 5.0,
              onChanged: (value) {
                setState(() {
                  timeDilation = value != null && value ? 5.0 : 1.0;
                });
              },
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                ThemeSwitcher.switcher(
                  clipper: const ThemeSwitcherBoxClipper(),
                  builder: (context, switcher) {
                    return TapDownButton(
                      child: const Text('Box Animation'),
                      onTap: (details) {
                        switcher.changeTheme(
                          theme: ThemeModelInheritedNotifier.of(context)
                                      .theme
                                      .brightness ==
                                  Brightness.light
                              ? darkTheme
                              : lightTheme,
                          offset: details.localPosition,
                        );
                      },
                    );
                  },
                ),
                ThemeSwitcher(
                  clipper: const ThemeSwitcherCircleClipper(),
                  builder: (context) {
                    return TapDownButton(
                      child: const Text('Circle Animation'),
                      onTap: (details) {
                        ThemeSwitcher.of(context).changeTheme(
                          theme: ThemeModelInheritedNotifier.of(context)
                                      .theme
                                      .brightness ==
                                  Brightness.light
                              ? darkTheme
                              : lightTheme,
                          offset: details.localPosition,
                        );
                      },
                    );
                  },
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                ThemeSwitcher(
                  clipper: const ThemeSwitcherBoxClipper(),
                  builder: (context) {
                    return TapDownButton(
                      child: const Text('Box (Reverse)'),
                      onTap: (details) {
                        var brightness = ThemeModelInheritedNotifier.of(context)
                            .theme
                            .brightness;
                        ThemeSwitcher.of(context).changeTheme(
                          theme: brightness == Brightness.light
                              ? darkTheme
                              : lightTheme,
                          offset: details.localPosition,
                          isReversed:
                              brightness == Brightness.dark ? true : false,
                        );
                      },
                    );
                  },
                ),
                ThemeSwitcher(
                  clipper: const ThemeSwitcherCircleClipper(),
                  builder: (context) {
                    return TapDownButton(
                      child: const Text('Circle (Reverse)'),
                      onTap: (details) {
                        var brightness = ThemeModelInheritedNotifier.of(context)
                            .theme
                            .brightness;
                        ThemeSwitcher.of(context).changeTheme(
                          theme: brightness == Brightness.light
                              ? darkTheme
                              : lightTheme,
                          offset: details.localPosition,
                          isReversed:
                              brightness == Brightness.dark ? true : false,
                        );
                      },
                    );
                  },
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ThemeSwitcher(
                  builder: (context) {
                    return Checkbox(
                      value: ThemeModelInheritedNotifier.of(context).theme ==
                          pinkTheme,
                      onChanged: (needPink) {
                        ThemeSwitcher.of(context).changeTheme(
                          theme: needPink != null && needPink
                              ? pinkTheme
                              : lightTheme,
                        );
                      },
                    );
                  },
                ),
                ThemeSwitcher(
                  builder: (context) {
                    return Checkbox(
                      value: ThemeModelInheritedNotifier.of(context).theme ==
                          darkBlueTheme,
                      onChanged: (needDarkBlue) {
                        ThemeSwitcher.of(context).changeTheme(
                          theme: needDarkBlue != null && needDarkBlue
                              ? darkBlueTheme
                              : lightTheme,
                        );
                      },
                    );
                  },
                ),
                ThemeSwitcher(
                  builder: (context) {
                    return Checkbox(
                      value: ThemeModelInheritedNotifier.of(context).theme ==
                          halloweenTheme,
                      onChanged: (needBlue) {
                        ThemeSwitcher.of(context).changeTheme(
                          theme: needBlue != null && needBlue
                              ? halloweenTheme
                              : lightTheme,
                        );
                      },
                    );
                  },
                ),
              ],
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(
          Icons.add,
        ),
      ),
    );
  }
}

class TapDownButton extends StatelessWidget {
  const TapDownButton({
    Key? key,
    required this.onTap,
    required this.child,
  }) : super(key: key);

  final void Function(TapDownDetails details) onTap;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(
          vertical: 16.0,
          horizontal: 24.0,
        ),
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(
            Radius.circular(24.0),
          ),
          border: Border.all(width: 1.0),
        ),
        child: child,
      ),
    );
  }
}
NamanShergill commented 9 months ago

I have been trying to debug the cause of this for the last few hours, but I am lost. Any ideas?

NamanShergill commented 9 months ago

I have learned a very valuable lesson in the difference between GlobalKey and ValueKey today. I was supposed to use the former to preserve the widget state on being moved in the tree. Can't believe the fix was so simple.