diff --git a/packages/yak_flutter/CHANGELOG.md b/packages/yak_flutter/CHANGELOG.md index d6a24979..454290a3 100644 --- a/packages/yak_flutter/CHANGELOG.md +++ b/packages/yak_flutter/CHANGELOG.md @@ -1,5 +1,6 @@ ### 3.0.2 -- add `EdgePositioned`, `ScalebleWidget`, `MediaQueryWidget` and `ScalableEdgePositioned` +- add `EdgePositioned`, `ScalebleWidget`, `MediaQueryWidget` , +- `InheritedRestrictedNotifier` and `ScalableEdgePositioned` ### 3.0.0 - rework due to yak_result v3 diff --git a/packages/yak_flutter/lib/src/widgets/all.dart b/packages/yak_flutter/lib/src/widgets/all.dart index 9c29e59e..8552d04d 100644 --- a/packages/yak_flutter/lib/src/widgets/all.dart +++ b/packages/yak_flutter/lib/src/widgets/all.dart @@ -3,3 +3,4 @@ export 'media_query_widget.dart'; export 'preferred_size_themed.dart'; export 'scalable_edge_positioned.dart'; export 'scalable_widget.dart'; +export 'inherited_value_notifier.dart'; diff --git a/packages/yak_flutter/lib/src/widgets/inherited_value_notifier.dart b/packages/yak_flutter/lib/src/widgets/inherited_value_notifier.dart new file mode 100644 index 00000000..441b75b9 --- /dev/null +++ b/packages/yak_flutter/lib/src/widgets/inherited_value_notifier.dart @@ -0,0 +1,74 @@ +import 'package:flutter/widgets.dart'; + +typedef Initialize = T Function(); +typedef Dispose = void Function(T); + +typedef HandleListener = void Function(VoidCallback); + +extension type RestrictedNotifier(ValueNotifier notifier) { + HandleListener get addListener => notifier.addListener; + HandleListener get removeListener => notifier.removeListener; + T get value => notifier.value; + set value(T value) => notifier.value = value; +} + +class InheritedRestrictedNotifier extends InheritedWidget { + const InheritedRestrictedNotifier({ + super.key, + required this.notifier, + required super.child, + }); + + final RestrictedNotifier notifier; + + factory InheritedRestrictedNotifier.of(BuildContext context) { + final inherited = context + .dependOnInheritedWidgetOfExactType>(); + if (inherited == null) { + throw Exception( + 'InheritedRestrictedNotifier<$T> not found in BuildContext'); + } + return inherited; + } + + @override + bool updateShouldNotify(InheritedRestrictedNotifier oldWidget) => false; +} + +class InheritedRestrictedNotifierdWidget + extends StatefulWidget { + final Widget child; + final T value; + + const InheritedRestrictedNotifierdWidget({ + super.key, + required this.child, + required this.value, + }); + + @override + State> createState() => + _InheritedRestrictedNotifierdWidgettState(); +} + +class _InheritedRestrictedNotifierdWidgettState + extends State> { + late final ValueNotifier notifier; + @override + void initState() { + notifier = ValueNotifier(widget.value); + super.initState(); + } + + @override + void dispose() { + notifier.dispose(); + super.dispose(); + } + + @override + Widget build(context) => InheritedRestrictedNotifier( + notifier: RestrictedNotifier(notifier), + child: widget.child, + ); +}