Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

1.6 KB
Raw Blame
import 'package:flutter/widgets.dart';

class SettingsStateContainer extends StatefulWidget {
  const SettingsStateContainer({super.key, required this.child});
  final Widget child;

  @override
  State<SettingsStateContainer> createState() => _SettingsState();
}

class _SettingsState extends State<SettingsStateContainer> {
  int _expandedSectionCount = 0;

  void increment() {
    setState(() {
      _expandedSectionCount += 1;
    });
  }

  void decrement() {
    setState(() {
      _expandedSectionCount -= 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return InheritedSettingsState(
      expandedSectionCount: _expandedSectionCount,
      increment: increment,
      decrement: decrement,
      child: widget.child,
    );
  }
}

class InheritedSettingsState extends InheritedWidget {
  final int expandedSectionCount;
  final void Function() increment;
  final void Function() decrement;

  const InheritedSettingsState({
    super.key,
    required this.expandedSectionCount,
    required this.increment,
    required this.decrement,
    required super.child,
  });

  bool get isAnySectionExpanded => expandedSectionCount > 0;

  static InheritedSettingsState of(BuildContext context) =>
      context.dependOnInheritedWidgetOfExactType<InheritedSettingsState>()!;

  static InheritedSettingsState? maybeOf(BuildContext context) =>
      context.dependOnInheritedWidgetOfExactType<InheritedSettingsState>();

  @override
  bool updateShouldNotify(covariant InheritedSettingsState oldWidget) {
    return isAnySectionExpanded != oldWidget.isAnySectionExpanded;
  }
}