Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"pages": [
"widgets/custom_scroll_view",
"widgets/grid_view",
"widgets/selectable_text",
"widgets/table",
"widgets/table_cell",
"widgets/table_row",
Expand Down
51 changes: 51 additions & 0 deletions docs/widgets/selectable_text.mdx
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"
}
```
2 changes: 1 addition & 1 deletion examples/counter_example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
stac: ^0.10.0
stac:
freezed_annotation: ^3.0.0
json_annotation: ^4.9.0
flutter_bloc: ^9.1.0
Expand Down
4 changes: 2 additions & 2 deletions examples/movie_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
stac: ^1.0.1
stac_core: ^1.0.0
stac:
stac_core:
dio: ^5.8.0+1
smooth_page_indicator: ^1.2.1
json_annotation: ^4.9.0
Expand Down
22 changes: 22 additions & 0 deletions examples/stac_gallery/assets/json/home_screen.json
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,28 @@
}
}
},
{
"type": "listTile",
"leading": {
"type": "icon",
"icon": "text_fields"
},
"title": {
"type": "text",
"data": "Stac Selectable Text"
},
"subtitle": {
"type": "text",
"data": "A run of text with a single style that can be selected"
},
"onTap": {
"actionType": "navigate",
"widgetJson": {
"type": "exampleScreen",
"assetPath": "assets/json/selectable_text_example.json"
}
}
},
{
"type": "listTile",
"leading": {
Expand Down
117 changes: 117 additions & 0 deletions examples/stac_gallery/assets/json/selectable_text_example.json
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
}
]
}
}
}
}
4 changes: 2 additions & 2 deletions examples/stac_gallery/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ dependencies:
freezed_annotation: ^3.0.0
json_annotation: ^4.9.0
flutter_bloc: ^9.1.0
stac: ^0.10.0
stac_webview: ^0.1.2
stac:
stac_webview:
stac_core:

dev_dependencies:
Expand Down
1 change: 1 addition & 0 deletions packages/stac/lib/src/framework/stac_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class StacService {
const StacVisibilityParser(),
const StacBackdropFilterParser(),
const StacVerticalDividerParser(),
const StacSelectableTextParser(),
];

static final _actionParsers = <StacActionParser>[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_action_parser.dart';
import 'package:stac/stac.dart';
import 'package:stac_core/stac_core.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_widget_parser.dart';
import 'package:stac/src/parsers/foundation/geometry/stac_edge_insets_parser.dart';
import 'package:stac/src/parsers/foundation/layout/stac_clip_parser.dart';
import 'package:stac/src/parsers/foundation/text/stac_text_style_parser.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_widget_parser.dart';
import 'package:stac/src/parsers/foundation/alignment/stac_alignment_parser.dart';
import 'package:stac/src/parsers/foundation/decoration/stac_box_decoration_parser.dart';
import 'package:stac/src/parsers/foundation/geometry/stac_box_constraints_parser.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_action_parser.dart';
import 'package:stac/src/parsers/core/stac_widget_parser.dart';
import 'package:stac/src/parsers/foundation/animation/stac_duration_parsers.dart';
import 'package:stac/src/parsers/foundation/borders/stac_border_radius_parser.dart';
import 'package:stac/src/parsers/foundation/borders/stac_shape_border_parser.dart';
Expand Down
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,
);
}

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,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_action_parser.dart';
import 'package:stac/src/parsers/core/stac_widget_parser.dart';
import 'package:stac/src/parsers/foundation/interaction/stac_drag_start_behavior_parser.dart';
import 'package:stac/src/parsers/foundation/layout/stac_material_tap_target_size_parser.dart';
import 'package:stac/stac.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/stac/lib/src/parsers/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export 'package:stac/src/parsers/widgets/stac_radio_group/stac_radio_group_parse
export 'package:stac/src/parsers/widgets/stac_refresh_indicator/stac_refresh_indicator_parser.dart';
export 'package:stac/src/parsers/widgets/stac_safe_area/stac_safe_area_parser.dart';
export 'package:stac/src/parsers/widgets/stac_scaffold/stac_scaffold_parser.dart';
export 'package:stac/src/parsers/widgets/stac_selectable_text/stac_selectable_text_parser.dart';
export 'package:stac/src/parsers/widgets/stac_set_value/stac_set_value_parser.dart';
export 'package:stac/src/parsers/widgets/stac_single_child_scroll_view/stac_single_child_scroll_view_parser.dart';
export 'package:stac/src/parsers/widgets/stac_sized_box/stac_sized_box_parser.dart';
Expand Down
Loading