This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
-
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.
- Loading branch information
Showing
9 changed files
with
111 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include: package:flutter_lints/flutter.yaml | ||
include: package:strict_lints/strict_lints.yaml |
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
/// A wrap of generic on the [InheritedWidget] widget. | ||
/// | ||
/// 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. | ||
class InheritedData<T> extends InheritedWidget { | ||
/// 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]). | ||
/// | ||
/// 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 { | ||
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!; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
name: wrap | ||
description: Chain style programming syntax sugar utilities for flutter widgets. | ||
version: 0.5.2 | ||
version: 0.6.0 | ||
repository: https://github.com/aprosail/wrap | ||
environment: {sdk: ">=3.3.4 <4.0.0", flutter: ">=3.19.6"} | ||
topics: | ||
- syntax-sugar | ||
- code-style | ||
- chain-style | ||
- chain-programming | ||
- chain-style-programming | ||
- flutter-widgets | ||
- flutter-data-inherit | ||
|
||
dependencies: | ||
flutter: {sdk: flutter} | ||
|
||
dev_dependencies: | ||
flutter_test: {sdk: flutter} | ||
flutter_lints: ^3.0.0 | ||
strict_lints: ^0.1.1 |
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,17 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:wrap/wrap.dart'; | ||
|
||
void main() { | ||
testWidgets('inherit and find', (t) async { | ||
const message = 'it works'; | ||
await t.pumpWidget( | ||
ReContext( | ||
(context) => Text(context.findAndTrust<String>()) | ||
.wrapCenter | ||
.ensureTextEnvironment(context), | ||
).wrapInherit(message), | ||
); | ||
expect(find.text(message), findsOneWidget); | ||
}); | ||
} |