Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

Commit

Permalink
wrap font and text
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed May 16, 2024
2 parents d84a852 + 825c777 commit 5c39ad2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.0

- Wrap string into text widget.
- Modification about text style in context.
- Optimize file structure and add corresponding tests.

## 0.3.0

- `ReContext` as a conciser way for `Builder`.
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,36 @@ class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) => const Text('app root')
Widget build(BuildContext context) => 'app root'
.textWidget
.wrapCenter
.wrapForeground(context, const Color(0xffdedcda))
.wrapBackground(const Color(0xff804044))
.ensureTextEnvironment(context);
}
```

Simplify your code is not the only benefit of using such package.
The chain-style programming also makes comment to cancel code easier:
such as you can simply cancel a line to remove a layer of widget,
and when you want to use them again, you can simply cancel the comment
to add them back to your code, that you don't need to worry about the indents.
For example:

```dart
import 'package:flutter/widgets.dart';
import 'package:wrap/wrap.dart';
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) => 'app root'
.textWidget
// .wrapCenter
// .wrapFontSize(23)
.wrapForeground(context, const Color(0xffbcbebf))
.wrapBackground(const Color(0xff232425))
.ensureTextEnvironment(context);
}
```
23 changes: 18 additions & 5 deletions lib/src/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ extension WrapTextEnvironment on Widget {
.ensureMedia(context);
}

extension StringToTextWidget on String {
/// Convert a [String] into a [Text] widget.
Widget get textWidget => Text(this);
}

extension WrapTextStyle on Widget {
/// If you want to modify based on current text style from context,
/// please consider [modifyTextStyle],
Expand All @@ -68,14 +73,20 @@ extension WrapTextStyle on Widget {
/// Calling those methods more than once might waste performance.
Widget wrapFontSize(BuildContext context, double size) {
assert(size > 0);
return modifyTextStyle(context, (style) => style.copyWith(fontSize: size));
return DefaultTextStyle(
style: DefaultTextStyle.of(context).style.copyWith(fontSize: size),
child: this,
);
}

/// Same as copy and modify the font family of [DefaultTextStyle.style]
/// and wrap this widget with such data.
/// Calling those methods more than once might waste performance.
Widget wrapFontFamily(BuildContext context, String family) =>
modifyTextStyle(context, (style) => style.copyWith(fontFamily: family));
DefaultTextStyle(
style: DefaultTextStyle.of(context).style.copyWith(fontFamily: family),
child: this,
);

/// Same as copy and modify the font family fallback
/// of [DefaultTextStyle.style]
Expand All @@ -85,9 +96,11 @@ extension WrapTextStyle on Widget {
BuildContext context,
List<String> fontFamilyFallback,
) =>
modifyTextStyle(
context,
(style) => style.copyWith(fontFamilyFallback: fontFamilyFallback),
DefaultTextStyle(
style: DefaultTextStyle.of(context)
.style
.copyWith(fontFamilyFallback: fontFamilyFallback),
child: this,
);

/// Same as copy and modify the color of [DefaultTextStyle.style]
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: wrap
description: Chain style programming syntax sugar utilities for flutter widgets.
version: 0.3.0
version: 0.4.0
repository: https://github.com/aprosail/wrap
environment: {sdk: ">=3.3.4 <4.0.0", flutter: ">=3.19.6"}

Expand Down
28 changes: 28 additions & 0 deletions test/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,32 @@ void main() {
));
expect(find.text(message), findsOneWidget);
});

testWidgets('modify text style', (t) async {
const fontColor = Color(0xff804044);
const fontSize = 23.4;
const fontFamily = 'iosevka';
await t.pumpWidget(
ReContext((context) {
final font = DefaultTextStyle.of(context).style;
return <Widget>[
'font color: ${font.color}'.textWidget,
'font size: ${font.fontSize}'.textWidget,
'font family: ${font.fontFamily}'.textWidget,
].centerColumn();
}).wrap((context, child) => child
.modifyTextStyle(
context,
(raw) => raw.copyWith(
color: fontColor,
fontSize: fontSize,
fontFamily: fontFamily,
),
)
.ensureTextEnvironment(context)),
);
expect(find.text('font color: $fontColor'), findsOneWidget);
expect(find.text('font size: $fontSize'), findsOneWidget);
expect(find.text('font family: $fontFamily'), findsOneWidget);
});
}

0 comments on commit 5c39ad2

Please sign in to comment.