mirrored_repositories/Flutter_Hacktoberfest_2021/CoviCo/lib/ui
import 'package:flutter/material.dart';

class ThemeSwitcher extends InheritedWidget {
  final _ThemeSwitcherWidgetState
      data; // We'll use ThemeSwitcher to get access to the current state of ThemeSwitcherWidget

  const ThemeSwitcher({
    Key key,
    @required this.data,
    @required Widget child,
  })  : assert(child != null),
        super(key: key, child: child);

  static _ThemeSwitcherWidgetState of(BuildContext context) {
    //This method returns the current state of the ThemeSwitcherWidget. This will be used down the tree
    return (context
        .dependOnInheritedWidgetOfExactType<ThemeSwitcher>(
            aspect: ThemeSwitcher)
        .data);
  }

  @override
  bool updateShouldNotify(ThemeSwitcher old) {
    return this != old;
  }
}

class ThemeSwitcherWidget extends StatefulWidget {
  final bool initialDarkModeOn; // this is the initial state of the variable
  final Widget
      child; // child to which this boolean variable should be propagated upon change. This will be our app in this case

  ThemeSwitcherWidget({Key key, this.initialDarkModeOn, this.child})
      : assert(initialDarkModeOn != null),
        assert(child != null),
        super(key: key);

  @override
  _ThemeSwitcherWidgetState createState() => _ThemeSwitcherWidgetState();
}

class _ThemeSwitcherWidgetState extends State<ThemeSwitcherWidget> {
  bool isDarkModeOn;

  void switchDarkMode() {
    //method used to toggle dark mode during the runtime of the app
    setState(() {
      isDarkModeOn = !isDarkModeOn;
    });
  }

  @override
  Widget build(BuildContext context) {
    isDarkModeOn = isDarkModeOn ??
        widget
            .initialDarkModeOn; // this is the build method which would build the widget tree with the above info
    return ThemeSwitcher(
      data: this,
      child: widget.child,
    );
  }
}
