Skip to content

Commit

Permalink
inherit on change trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed Jun 29, 2024
1 parent 65c6759 commit ced1424
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- Theme template and theme handler.
- Support customized theme on mixin.
- Theme handler adapt system brightness change.
- Remove unnecessary encapsulations and tidy comments.
- Inherit on change trigger (experimental).

## 0.3.0

Expand Down
26 changes: 22 additions & 4 deletions lib/src/inherit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ extension FindInherit on BuildContext {
}

/// A stateful widget to handle [Inherit]ed data in widget tree.
/// You can change the handled data from the descendants in the widget tree
/// using the [BuildContext]'s [InheritHandlerAPI.update] extension method.
///
/// 1. You can change the handled data from the descendants in the widget tree
/// using the [BuildContext]'s [InheritHandlerAPI.update] extension method.
/// 2. You can also customize [onUpdate] callback to resolve
/// actions when the value changed.
///
/// It's strongly not recommended to use it directly,
/// please consider using [WrapInheritHandler.handle] extended on [Widget]
Expand All @@ -87,10 +90,12 @@ class InheritHandler<T> extends StatefulWidget {
/// before using such constructor directly.
const InheritHandler({
super.key,
this.onUpdate,
required this.data,
required this.child,
});

final void Function(T value)? onUpdate;
final T data;
final Widget child;

Expand All @@ -102,7 +107,9 @@ class _InheritHandlerState<T> extends State<InheritHandler<T>> {
late T _data = widget.data;

void update(T value) {
if (_data != value) setState(() => _data = value);
if (_data == value) return;
setState(() => _data = value);
widget.onUpdate?.call(value);
}

@override
Expand All @@ -127,7 +134,18 @@ class InheritHandlerAPI<T> {
}

extension WrapInheritHandler on Widget {
Widget handle<T>(T data) => InheritHandler<T>(data: data, child: this);
/// Handle a data of type [T] into the widget tree.
///
/// 1. This extension method is an encapsulation of [InheritHandler].
/// 2. You can use [UpdateInheritHandler.update] extension method
/// to modify the handled value.
/// 3. You can also specify [onUpdate] to register trigger actions
/// when the value changed.
Widget handle<T>(T data, {void Function(T)? onUpdate}) => InheritHandler<T>(
onUpdate: onUpdate,
data: data,
child: this,
);
}

extension UpdateInheritHandler on BuildContext {
Expand Down

0 comments on commit ced1424

Please sign in to comment.