Skip to content

Commit

Permalink
[yak_flutter] add inherited_value_notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Iapicca committed Apr 23, 2024
1 parent 0539209 commit 59b6b50
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/yak_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/yak_flutter/lib/src/widgets/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
74 changes: 74 additions & 0 deletions packages/yak_flutter/lib/src/widgets/inherited_value_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:flutter/widgets.dart';

typedef Initialize<T> = T Function();
typedef Dispose<T> = void Function(T);

typedef HandleListener = void Function(VoidCallback);

extension type RestrictedNotifier<T extends Object>(ValueNotifier<T> 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<T extends Object> extends InheritedWidget {
const InheritedRestrictedNotifier({
super.key,
required this.notifier,
required super.child,
});

final RestrictedNotifier<T> notifier;

factory InheritedRestrictedNotifier.of(BuildContext context) {
final inherited = context
.dependOnInheritedWidgetOfExactType<InheritedRestrictedNotifier<T>>();
if (inherited == null) {
throw Exception(
'InheritedRestrictedNotifier<$T> not found in BuildContext');
}
return inherited;
}

@override
bool updateShouldNotify(InheritedRestrictedNotifier<T> oldWidget) => false;
}

class InheritedRestrictedNotifierdWidget<T extends Object>
extends StatefulWidget {
final Widget child;
final T value;

const InheritedRestrictedNotifierdWidget({
super.key,
required this.child,
required this.value,
});

@override
State<InheritedRestrictedNotifierdWidget<T>> createState() =>
_InheritedRestrictedNotifierdWidgettState<T>();
}

class _InheritedRestrictedNotifierdWidgettState<T extends Object>
extends State<InheritedRestrictedNotifierdWidget<T>> {
late final ValueNotifier<T> notifier;
@override
void initState() {
notifier = ValueNotifier(widget.value);
super.initState();
}

@override
void dispose() {
notifier.dispose();
super.dispose();
}

@override
Widget build(context) => InheritedRestrictedNotifier<T>(
notifier: RestrictedNotifier(notifier),
child: widget.child,
);
}

0 comments on commit 59b6b50

Please sign in to comment.