-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[yak_flutter] add inherited_value_notifier
- Loading branch information
Francesco Iapicca
committed
Apr 23, 2024
1 parent
0539209
commit 59b6b50
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
74 changes: 74 additions & 0 deletions
74
packages/yak_flutter/lib/src/widgets/inherited_value_notifier.dart
This file contains 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
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, | ||
); | ||
} |