Skip to content

Commit

Permalink
Use SelectableText on RoundedBackgroundText.selectable
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa committed Jul 17, 2023
1 parent 24aab57 commit 4ba7578
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 28 deletions.
56 changes: 30 additions & 26 deletions lib/src/rounded_background_text.dart
Expand Up @@ -114,39 +114,43 @@ class RoundedBackgroundText extends StatelessWidget {
double textScaleFactor = 1.0,
double innerRadius = kDefaultInnerFactor,
double outerRadius = kDefaultOuterFactor,
MouseCursor? mouseCursor,
double cursorWidth = 2.0,
Color? cursorColor,
double? cursorHeight,
Radius? cursorRadius,
SelectionChangedCallback? onSelectionChanged,
bool enableInteractiveSelection = true,
String? semanticsLabel,
}) {
final controller = TextEditingController();
controller.text = text;
return RoundedBackgroundTextField(
key: key,
controller: controller,
focusNode: focusNode,
autofocus: autofocus,
style: style,
readOnly: true,
textDirection: textDirection,
backgroundColor: backgroundColor,
textAlign: textAlign,
textScaleFactor: textScaleFactor,
innerRadius: innerRadius,
outerRadius: outerRadius,
mouseCursor: mouseCursor,
autocorrect: false,
cursorColor: cursorColor,
cursorHeight: cursorHeight,
cursorRadius: cursorRadius,
cursorWidth: cursorWidth,
selectionControls: selectionControls,
onSelectionChanged: onSelectionChanged,
enableInteractiveSelection: enableInteractiveSelection,
);
return Stack(children: [
RoundedBackgroundText(
text,
style: style,
textDirection: textDirection,
textAlign: textAlign,
textScaleFactor: textScaleFactor,
innerRadius: innerRadius,
outerRadius: outerRadius,
backgroundColor: backgroundColor,
),
SelectableText(
text,
style: style,
textDirection: textDirection,
textAlign: textAlign,
textScaleFactor: textScaleFactor,
cursorColor: cursorColor,
cursorHeight: cursorHeight,
cursorRadius: cursorRadius,
cursorWidth: cursorWidth,
selectionControls: selectionControls,
onSelectionChanged: onSelectionChanged,
enableInteractiveSelection: enableInteractiveSelection,
focusNode: focusNode,
autofocus: autofocus,
semanticsLabel: semanticsLabel,
),
]);
}

/// Creates a selectable [RoundedBackgroundText] that can have multiple styles
Expand Down
3 changes: 2 additions & 1 deletion lib/src/rounded_background_text_field.dart
@@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Expand Down Expand Up @@ -426,7 +427,6 @@ class _RoundedBackgroundTextFieldState
return Stack(
alignment: Alignment.center,
children: [
const Positioned.fill(child: SizedBox.expand()),
if (textController.text.isNotEmpty)
Positioned.fill(
top: scrollController.hasClients
Expand All @@ -441,6 +441,7 @@ class _RoundedBackgroundTextFieldState
bottom: 3.0,
),
child: RoundedBackgroundText.rich(
key: ValueKey(textController.text),
text: textController.buildTextSpan(
context: context,
withComposing: !widget.readOnly,
Expand Down
67 changes: 66 additions & 1 deletion lib/src/rounded_background_text_span.dart
@@ -1,3 +1,5 @@
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'rounded_background_text.dart';

Expand All @@ -8,9 +10,11 @@ import 'rounded_background_text.dart';
/// * [TextSpan], a node that represents text in an [InlineSpan] tree.
/// * [RoundedBackgroundText], which renders rounded background texts
class RoundedBackgroundTextSpan extends WidgetSpan {
final String text;

/// Creates a text with rounded background with the given values
RoundedBackgroundTextSpan({
required String text,
required this.text,
TextStyle? style,
Color? backgroundColor,
double textScaleFactor = 1.0,
Expand All @@ -35,4 +39,65 @@ class RoundedBackgroundTextSpan extends WidgetSpan {
),
baseline: baseline,
);

TextSpan get _textSpan => TextSpan(text: text);

@override
void build(ui.ParagraphBuilder builder,
{double textScaleFactor = 1.0, List<PlaceholderDimensions>? dimensions}) {
super.build(
builder,
textScaleFactor: textScaleFactor,
dimensions: dimensions,
);
try {
builder.addText(text);
} on ArgumentError catch (exception, stack) {
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'painting library',
context: ErrorDescription('while building a TextSpan'),
));
// Use a Unicode replacement character as a substitute for invalid text.
builder.addText('\uFFFD');
}
}

@override
InlineSpan? getSpanForPositionVisitor(
TextPosition position, Accumulator offset) {
return _textSpan.getSpanForPositionVisitor(position, offset);
}

@override
void computeToPlainText(
StringBuffer buffer, {
bool includeSemanticsLabels = true,
bool includePlaceholders = true,
}) {
_textSpan.computeToPlainText(
buffer,
includeSemanticsLabels: includeSemanticsLabels,
includePlaceholders: includePlaceholders,
);
}

@override
void computeSemanticsInformation(
List<InlineSpanSemanticsInformation> collector, {
ui.Locale? inheritedLocale,
bool inheritedSpellOut = false,
}) {
return _textSpan.computeSemanticsInformation(
collector,
inheritedLocale: inheritedLocale,
inheritedSpellOut: inheritedSpellOut,
);
}

@override
int? codeUnitAtVisitor(int index, Accumulator offset) {
return _textSpan.codeUnitAtVisitor(index, offset);
}
}

0 comments on commit 4ba7578

Please sign in to comment.