Created with <3 with dartpad.dev.
Last active
October 10, 2023 08:08
-
-
Save andyduke/0874be0e9e838b69b09d3a3e75dbc012 to your computer and use it in GitHub Desktop.
Flutter: InheritedSettings concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:collection/collection.dart'; | |
| abstract interface class InheritedSettingsData {} | |
| class InheritedSettings extends InheritedWidget { | |
| final List<InheritedSettingsData> settings; | |
| const InheritedSettings({ | |
| super.key, | |
| required super.child, | |
| required this.settings, | |
| }); | |
| static T? maybeSettingsOf<T extends InheritedSettingsData>(BuildContext context) { | |
| final settings = context.dependOnInheritedWidgetOfExactType<InheritedSettings>()?.settings; | |
| return (settings?.firstWhereOrNull((s) => s is T) as T?); | |
| } | |
| static T settingsOf<T extends InheritedSettingsData>(BuildContext context, T defaultSettings) { | |
| return maybeSettingsOf<T>(context) ?? defaultSettings; | |
| } | |
| static List<InheritedSettingsData>? maybeOf(BuildContext context) { | |
| return context.dependOnInheritedWidgetOfExactType<InheritedSettings>()?.settings; | |
| } | |
| static List<InheritedSettingsData> of(BuildContext context) { | |
| final List<InheritedSettingsData>? result = maybeOf(context); | |
| assert(result != null, 'No InheritedSettings found in context'); | |
| return result!; | |
| } | |
| @override | |
| bool updateShouldNotify(InheritedSettings oldWidget) => settings != oldWidget.settings; | |
| } | |
| // --- | |
| class ActionButtonSettings implements InheritedSettingsData { | |
| static ActionButtonSettings defaultSettings = ActionButtonSettings( | |
| builder: (context, child, onPressed) => ElevatedButton( | |
| onPressed: onPressed, | |
| child: child, | |
| ), | |
| ); | |
| final Widget Function(BuildContext context, Widget child, VoidCallback? onPressed) builder; | |
| ActionButtonSettings({ | |
| required this.builder, | |
| }); | |
| } | |
| // --- | |
| class ActionButton extends StatelessWidget { | |
| final Widget child; | |
| final VoidCallback? onPressed; | |
| const ActionButton({ | |
| required this.child, | |
| required this.onPressed, | |
| }); | |
| @override | |
| Widget build(BuildContext context) { | |
| // final settings = InheritedSettings.maybeSettingsOf<ActionButtonSettings>(context) ?? ActionButtonSettings.defaultSettings; | |
| final settings = InheritedSettings.settingsOf<ActionButtonSettings>(context, ActionButtonSettings.defaultSettings); | |
| return ConstrainedBox( | |
| constraints: const BoxConstraints( | |
| minWidth: 200, | |
| ), | |
| child: settings.builder( | |
| context, | |
| child, | |
| onPressed, | |
| ), | |
| ); | |
| } | |
| } | |
| // --- | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| theme: ThemeData.light(), | |
| debugShowCheckedModeBanner: false, | |
| home: Scaffold( | |
| body: Center( | |
| child: Column( | |
| mainAxisSize: MainAxisSize.min, | |
| children: [ | |
| InheritedSettings( | |
| settings: [ | |
| ActionButtonSettings( | |
| builder: (context, child, onPressed) => OutlinedButton( | |
| onPressed: onPressed, | |
| child: child, | |
| ), | |
| ), | |
| ], | |
| child: MyWidget(), | |
| ), | |
| // | |
| const Divider(), | |
| // | |
| MyWidget(), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class MyWidget extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return ActionButton( | |
| onPressed: () {}, | |
| child: const Text('OK'), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment