-
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
2 changed files
with
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
/// An encapsulation of [Builder], also used as an alias. | ||
Widget builder(Widget Function(BuildContext context) builder) => | ||
Builder(builder: builder); | ||
|
||
extension WrapMedia on Widget { | ||
Widget wrapMedia(MediaQueryData data) => MediaQuery(data: data, child: this); | ||
|
||
/// Ensure that the inner widget can get [MediaQueryData] from the context. | ||
/// If there's no available [MediaQueryData], it will get from the [View]. | ||
Widget ensureMedia(BuildContext context) { | ||
final media = MediaQuery.maybeOf(context); | ||
return media == null | ||
? wrapMedia(MediaQueryData.fromView(View.of(context))) | ||
: this; | ||
} | ||
} | ||
|
||
extension WrapDirection on Widget { | ||
Widget wrapDirection(TextDirection direction) => | ||
Directionality(textDirection: direction, child: this); | ||
|
||
/// Ensure that the inner widget can get [TextDirection] from the context. | ||
/// You can specify the [defaultValue] | ||
/// if you prefer [TextDirection.rtl] as default. | ||
Widget ensureDirection( | ||
BuildContext context, { | ||
TextDirection defaultValue = TextDirection.ltr, | ||
}) { | ||
final direction = Directionality.maybeOf(context); | ||
return direction == null ? wrapDirection(defaultValue) : 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