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
11 changed files
with
96 additions
and
32 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,16 +1,28 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
extension WrapContext on Widget { | ||
/// Same as wrapping current widget with a [Builder], which keeps chain style. | ||
Widget wrap(Widget Function(BuildContext context, Widget child) builder) => | ||
ReContext((context) => builder(context, this)); | ||
Builder(builder: (context) => builder(context, this)); | ||
} | ||
|
||
/// Refresh context, same as [Builder] but with conciser API. | ||
class ReContext extends StatelessWidget { | ||
const ReContext(this.builder, {super.key}); | ||
/// An encapsulation of [Builder], which makes it conciser. | ||
Widget builder(Widget Function(BuildContext context) builder) => | ||
Builder(builder: builder); | ||
|
||
final Widget Function(BuildContext context) builder; | ||
extension WrapMedia on Widget { | ||
Widget wrapMedia(MediaQueryData data) => MediaQuery(data: data, child: this); | ||
|
||
@override | ||
Widget build(BuildContext context) => builder(context); | ||
/// Ensure the widget is wrapped by a [MediaQuery] ancestor. | ||
/// | ||
/// Provide environment for [MediaQuery.of] that many widgets need, | ||
/// including the [Text] widget. Without such ancestor, | ||
/// once displaying a [Text], | ||
/// it will throw exceptions. | ||
Widget ensureMedia(BuildContext context) { | ||
final media = MediaQuery.maybeOf(context); | ||
return media == null | ||
? wrapMedia(MediaQueryData.fromView(View.of(context))) | ||
: this; | ||
} | ||
} |
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,58 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'inherit.dart'; | ||
|
||
class InheritHandler<T> extends StatefulWidget { | ||
const InheritHandler({ | ||
super.key, | ||
required this.data, | ||
required this.child, | ||
}); | ||
|
||
final T data; | ||
final Widget child; | ||
|
||
@override | ||
State<InheritHandler<T>> createState() => _InheritHandlerState<T>(); | ||
} | ||
|
||
class _InheritHandlerState<T> extends State<InheritHandler<T>> { | ||
late var _data = widget.data; | ||
T get data => _data; | ||
set data(T value) { | ||
if (_data != value) setState(() => _data = value); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant InheritHandler<T> oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
data = widget.data; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => widget.child | ||
.wrapInherit(_data) | ||
.wrapInheritUnchanged(InheritHandlerCaller<T>( | ||
updater: (data) => this.data = data, | ||
)); | ||
} | ||
|
||
class InheritHandlerCaller<T> { | ||
const InheritHandlerCaller({required this.updater}); | ||
|
||
final void Function(T data) updater; | ||
} | ||
|
||
extension WrapHandler on Widget { | ||
Widget wrapHandler<T>(T data) => InheritHandler<T>(data: data, child: this); | ||
} | ||
|
||
extension UpdateHandlerData on BuildContext { | ||
void maybeUpdate<T>(T data) => find<InheritHandlerCaller<T>>()?.updater(data); | ||
|
||
void updateAndTrust<T>(T data) => | ||
findAndTrust<InheritHandlerCaller<T>>().updater(data); | ||
|
||
void updateAndCheck<T>(T data) => | ||
findAndCheck<InheritHandlerCaller<T>>().updater(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 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
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
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