12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import 'package:flutter/widgets.dart';
- /// StatefulWidget that wraps InheritedSettingsState
- class SettingsStateContainer extends StatefulWidget {
- const SettingsStateContainer({
- Key? key,
- required this.child,
- }) : super(key: key);
- 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,
- );
- }
- }
- /// Keep track of the number of expanded sections in an entire menu tree.
- ///
- /// Since this is an InheritedWidget, subsections can obtain it from the context
- /// and use the current expansion state to style themselves differently if
- /// needed.
- ///
- /// Example usage:
- ///
- /// InheritedSettingsState.of(context).increment()
- ///
- class InheritedSettingsState extends InheritedWidget {
- final int expandedSectionCount;
- final void Function() increment;
- final void Function() decrement;
- const InheritedSettingsState({
- Key? key,
- required this.expandedSectionCount,
- required this.increment,
- required this.decrement,
- required Widget child,
- }) : super(key: key, child: 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;
- }
- }
|