-
-
Notifications
You must be signed in to change notification settings - Fork 94
feat: add SelectableText widget and parser with documentation and exa… #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8660f6
feat: add SelectableText widget and parser with documentation and exa…
xkaper001 d9890d7
feat: update SelectableText widget to deprecate textScaleFactor and i…
xkaper001 1e42215
feat: add `package` property to `StacSelectableText` and generate JSO…
xkaper001 3378c73
refactor: remove unused imports from various parser files
xkaper001 b76c463
feat: Add JSON serialization for `StacSelectableText` and remove the …
xkaper001 74f8288
Merge branch 'dev' into issue385
xkaper001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,51 @@ | ||
| --- | ||
| title: "SelectableText" | ||
| description: "Documentation for SelectableText" | ||
| --- | ||
|
|
||
| Stac SelectableText allows you to build the Flutter SelectableText widget using JSON. | ||
| To know more about the SelectableText widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/material/SelectableText-class.html). | ||
|
|
||
| ## Properties | ||
|
|
||
| | Property | Type | Description | | ||
| |----------------------------|----------------------|---------------------------------------------------| | ||
| | data | `String` | The text to display. | | ||
| | children | `List<StacTextSpan>` | The list of text spans to display. | | ||
| | style | `StacTextStyle?` | The style to apply to the text. | | ||
| | textAlign | `TextAlign?` | The alignment of the text. | | ||
| | textDirection | `TextDirection?` | The direction of the text. | | ||
| | textScaleFactor | `double?` | **Deprecated.** Use `textScaler` instead. | | ||
| | textScaler | `double?` | The font scaling strategy to use. | | ||
| | showCursor | `bool?` | Whether to show the cursor. | | ||
| | autofocus | `bool?` | Whether to autofocus the widget. | | ||
| | minLines | `int?` | The minimum number of lines to display. | | ||
| | maxLines | `int?` | The maximum number of lines to display. | | ||
| | cursorWidth | `double?` | The width of the cursor. | | ||
| | cursorHeight | `double?` | The height of the cursor. | | ||
| | cursorRadius | `double?` | The radius of the cursor. | | ||
| | cursorColor | `String?` | The color of the cursor. | | ||
| | enableInteractiveSelection | `bool?` | Whether to enable interactive selection. | | ||
| | onTap | `StacAction?` | The action to perform when the text is tapped. | | ||
| | semanticsLabel | `String?` | The semantics label for the text. | | ||
| | textWidthBasis | `TextWidthBasis?` | The width basis for the text. | | ||
| | selectionColor | `String?` | The color of the text selection. | | ||
|
|
||
| > [!NOTE] | ||
| > `textScaleFactor` is deprecated. Use `textScaler` instead. | ||
| > Example migration: `textScaler: 1.5` corresponds to `TextScaler.linear(1.5)`. | ||
|
|
||
| ## Example JSON | ||
|
|
||
| ```json | ||
| { | ||
| "type": "selectableText", | ||
| "data": "This is a selectable text", | ||
| "style": { | ||
| "fontSize": 18, | ||
| "fontWeight": "w600" | ||
| }, | ||
| "showCursor": true, | ||
| "cursorColor": "#FF0000" | ||
| } | ||
| ``` |
This file contains hidden or 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 hidden or 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 hidden or 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
117 changes: 117 additions & 0 deletions
117
examples/stac_gallery/assets/json/selectable_text_example.json
This file contains hidden or 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,117 @@ | ||
| { | ||
| "type": "scaffold", | ||
| "appBar": { | ||
| "type": "appBar", | ||
| "title": { | ||
| "type": "text", | ||
| "data": "Selectable Text" | ||
| } | ||
| }, | ||
| "body": { | ||
| "type": "singleChildScrollView", | ||
| "child": { | ||
| "type": "padding", | ||
| "padding": { | ||
| "top": 12, | ||
| "left": 12, | ||
| "right": 12, | ||
| "bottom": 12 | ||
| }, | ||
| "child": { | ||
| "type": "column", | ||
| "crossAxisAlignment": "start", | ||
| "children": [ | ||
| { | ||
| "type": "text", | ||
| "data": "Standard Selectable Text", | ||
| "style": { | ||
| "fontSize": 18, | ||
| "fontWeight": "w600" | ||
| } | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 10 | ||
| }, | ||
| { | ||
| "type": "selectableText", | ||
| "data": "You can select this text. Long press or double tap to select." | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 32 | ||
| }, | ||
| { | ||
| "type": "text", | ||
| "data": "Rich Selectable Text", | ||
| "style": { | ||
| "fontSize": 18, | ||
| "fontWeight": "w600" | ||
| } | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 10 | ||
| }, | ||
| { | ||
| "type": "selectableText", | ||
| "data": "This is a ", | ||
| "children": [ | ||
| { | ||
| "text": "selectable rich text.", | ||
| "style": { | ||
| "fontWeight": "w800", | ||
| "color": "#6700A4" | ||
| } | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 32 | ||
| }, | ||
| { | ||
| "type": "text", | ||
| "data": "Custom Cursor Selectable Text", | ||
| "style": { | ||
| "fontSize": 18, | ||
| "fontWeight": "w600" | ||
| } | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 10 | ||
| }, | ||
| { | ||
| "type": "selectableText", | ||
| "data": "This text has a red cursor.", | ||
| "showCursor": true, | ||
| "cursorColor": "#FF0000", | ||
| "cursorWidth": 5.0 | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 32 | ||
| }, | ||
| { | ||
| "type": "text", | ||
| "data": "Interactive Selection Disabled", | ||
| "style": { | ||
| "fontSize": 18, | ||
| "fontWeight": "w600" | ||
| } | ||
| }, | ||
| { | ||
| "type": "sizedBox", | ||
| "height": 10 | ||
| }, | ||
| { | ||
| "type": "selectableText", | ||
| "data": "You cannot select this text (interactive selection disabled).", | ||
| "enableInteractiveSelection": false | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
1 change: 0 additions & 1 deletion
1
packages/stac/lib/src/parsers/actions/stac_set_value/stac_set_value_action_parser.dart
This file contains hidden or 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
1 change: 0 additions & 1 deletion
1
packages/stac/lib/src/parsers/widgets/stac_app_bar/stac_app_bar_parser.dart
This file contains hidden or 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
1 change: 0 additions & 1 deletion
1
packages/stac/lib/src/parsers/widgets/stac_container/stac_container_parser.dart
This file contains hidden or 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
2 changes: 0 additions & 2 deletions
2
packages/stac/lib/src/parsers/widgets/stac_inkwell/stac_inkwell_parser.dart
This file contains hidden or 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
104 changes: 104 additions & 0 deletions
104
packages/stac/lib/src/parsers/widgets/stac_selectable_text/stac_selectable_text_parser.dart
This file contains hidden or 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,104 @@ | ||
| import 'package:flutter/gestures.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:stac/src/framework/framework.dart'; | ||
| import 'package:stac/src/parsers/foundation/text/stac_text_align_parser.dart'; | ||
| import 'package:stac/src/parsers/foundation/text/stac_text_direction_parser.dart'; | ||
| import 'package:stac/src/parsers/foundation/text/stac_text_style_parser.dart'; | ||
| import 'package:stac/src/parsers/foundation/text/stac_text_width_basis_parser.dart'; | ||
| import 'package:stac/src/utils/color_utils.dart'; | ||
| import 'package:stac_core/foundation/specifications/widget_type.dart'; | ||
| import 'package:stac_core/foundation/text/stac_text_style/stac_text_style.dart'; | ||
| import 'package:stac_core/widgets/selectable_text/stac_selectable_text.dart'; | ||
| import 'package:stac_framework/stac_framework.dart'; | ||
|
|
||
| class StacSelectableTextParser extends StacParser<StacSelectableText> { | ||
| const StacSelectableTextParser(); | ||
|
|
||
| @override | ||
| String get type => WidgetType.selectableText.name; | ||
|
|
||
| @override | ||
| StacSelectableText getModel(Map<String, dynamic> json) => | ||
| StacSelectableText.fromJson(json); | ||
|
|
||
| @override | ||
| Widget parse(BuildContext context, StacSelectableText model) { | ||
| return SelectableText.rich( | ||
| _buildTextSpan(context, model), | ||
| onTap: model.onTap != null | ||
| ? () => Stac.onCallFromJson(model.onTap?.toJson(), context) | ||
| : null, | ||
| style: _resolveStyle(context, model.style, model.copyWithStyle), | ||
| textAlign: model.textAlign?.parse, | ||
| textDirection: model.textDirection?.parse, | ||
| textScaler: model.textScaler != null | ||
| ? TextScaler.linear(model.textScaler!) | ||
| : null, | ||
| showCursor: model.showCursor ?? false, | ||
| autofocus: model.autofocus ?? false, | ||
| minLines: model.minLines, | ||
| maxLines: model.maxLines, | ||
| cursorWidth: model.cursorWidth ?? 2.0, | ||
| cursorHeight: model.cursorHeight, | ||
| cursorRadius: model.cursorRadius != null | ||
| ? Radius.circular(model.cursorRadius!) | ||
| : null, | ||
| cursorColor: model.cursorColor?.toColor(context), | ||
| selectionColor: model.selectionColor?.toColor(context), | ||
| enableInteractiveSelection: model.enableInteractiveSelection ?? true, | ||
| semanticsLabel: model.semanticsLabel, | ||
| textWidthBasis: model.textWidthBasis?.parse, | ||
| ); | ||
xkaper001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| TextSpan _buildTextSpan(BuildContext context, StacSelectableText model) { | ||
| var children = model.children ?? []; | ||
| return TextSpan( | ||
| text: model.data, | ||
| children: children.map((child) { | ||
| return TextSpan( | ||
| text: child.text, | ||
| style: child.style?.parse(context), | ||
| recognizer: child.onTap != null | ||
| ? (TapGestureRecognizer() | ||
| ..onTap = () => Stac.onCallFromJson(child.onTap, context)) | ||
| : null, | ||
| ); | ||
| }).toList(), | ||
| ); | ||
| } | ||
|
|
||
| TextStyle? _resolveStyle( | ||
| BuildContext context, | ||
| StacTextStyle? base, | ||
| StacCustomTextStyle? override, | ||
| ) { | ||
| final baseStyle = base?.parse(context); | ||
| if (override == null) return baseStyle; | ||
|
|
||
| final overrideParsed = override.parse(context); | ||
| if (overrideParsed == null) return baseStyle; | ||
| if (baseStyle == null) return overrideParsed; | ||
|
|
||
| return baseStyle.copyWith( | ||
| inherit: override.inherit, | ||
| color: overrideParsed.color, | ||
| backgroundColor: overrideParsed.backgroundColor, | ||
| fontSize: overrideParsed.fontSize, | ||
| fontWeight: overrideParsed.fontWeight, | ||
| fontStyle: overrideParsed.fontStyle, | ||
| letterSpacing: overrideParsed.letterSpacing, | ||
| wordSpacing: overrideParsed.wordSpacing, | ||
| textBaseline: overrideParsed.textBaseline, | ||
| height: overrideParsed.height, | ||
| leadingDistribution: overrideParsed.leadingDistribution, | ||
| decorationColor: overrideParsed.decorationColor, | ||
| decorationStyle: overrideParsed.decorationStyle, | ||
| decorationThickness: overrideParsed.decorationThickness, | ||
| debugLabel: overrideParsed.debugLabel, | ||
| fontFamily: overrideParsed.fontFamily, | ||
| fontFamilyFallback: overrideParsed.fontFamilyFallback, | ||
| overflow: overrideParsed.overflow, | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
xkaper001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
2 changes: 0 additions & 2 deletions
2
packages/stac/lib/src/parsers/widgets/stac_switch/stac_switch_parser.dart
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.