Skip to content

Commit

Permalink
sync version 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed Jun 27, 2024
2 parents e09b345 + 2a675df commit 2e42833
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.2.0
## 0.1.1

- Wrap center align to parent.
- Inherit handle and test (experimental).
- Add examples to compare before and after.

## 0.1.0
Expand Down
1 change: 1 addition & 0 deletions lib/modifier.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/inherit.dart';
export 'src/wrap.dart';
78 changes: 78 additions & 0 deletions lib/src/inherit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:flutter/widgets.dart';

class InheritedData<T> extends InheritedWidget {
/// A wrap of generic on the [InheritedWidget] widget.
///
/// 1. Register an inherited [data] into the widget tree.
/// That all its descendants can access the [data]
/// by calling the [FindInherit.find] method
/// (an extension on [BuildContext]).
/// 2. As it extends the [InheritedWidget] it can also
/// pass data to the descendants in the widget tree,
/// and let all related widget to re-renderer when the data changed.
/// But it let all similar inherit data to share the code
/// rather than inherit raw [InheritedWidget] once and once again.
/// 3. It's more recommended to use [WrapInherit.wrapInherit]
/// (an extension on [Widget])
/// rather than calling this constructor directly.
const InheritedData({
super.key,
required this.data,
required super.child,
});

final T data;

@override
bool updateShouldNotify(covariant InheritedData<T> oldWidget) =>
this.data != oldWidget.data;
}

class InheritedUnchangedData<T> extends InheritedWidget {
/// Similar to [InheritedData], but the [data] will never change.
///
/// 1. Register an unchanged data into the widget tree.
/// That all its descendants can access the [data]
/// by calling the [FindInherit.find] method
/// (an extension on [BuildContext]).
/// 2. This widget is designed to handle apis such as static functions,
/// which will not change during the whole life cycle.
/// 3. It's more recommended to use [WrapInherit.wrapInheritUnchanged]
/// (an extension on [Widget])
/// rather than calling this constructor directly.
const InheritedUnchangedData({
super.key,
required this.data,
required super.child,
});

final T data;

@override
bool updateShouldNotify(covariant InheritedWidget oldWidget) => false;
}

extension WrapInherit on Widget {
Widget wrapInherit<T>(T data) => InheritedData<T>(data: data, child: this);

Widget wrapInheritUnchanged<T>(T data) =>
InheritedUnchangedData<T>(data: data, child: this);
}

extension FindInherit on BuildContext {
T? find<T>() => dependOnInheritedWidgetOfExactType<InheritedData<T>>()?.data;

T findAndDefault<T>(T defaultValue) => find<T>() ?? defaultValue;

T findAndTrust<T>() {
final data = find<T>();
assert(data != null, 'cannot find $T in context');
return data!;
}

T findAndCheck<T>() {
final data = find<T>();
if (data == null) throw Exception('cannot find $T in context');
return data;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: modifier
description: Syntax sugar optimizations to avoid nesting hell in Flutter.
version: 0.1.0
version: 0.1.1
homepage: https://github.com/treeinfra/modifier
environment: {sdk: ">=3.4.3 <4.0.0", flutter: ">=3.22.2"}
topics:
Expand Down
18 changes: 18 additions & 0 deletions test/inherit_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:modifier/modifier.dart';

void main() {
testWidgets('inherit and find', (t) async {
const message = 'it works';
await t.pumpWidget(
builder(
(context) => Text(context.findAndTrust<String>())
.center
.ensureDirection(context)
.ensureMedia(context),
).wrapInherit(message),
);
expect(find.text(message), findsOneWidget);
});
}

0 comments on commit 2e42833

Please sign in to comment.