Skip to content
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

[SuperTextField] Add support for inline widgets (Resolves #2507) #2520

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:attributed_text/attributed_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:follow_the_leader/follow_the_leader.dart';
Expand Down Expand Up @@ -34,6 +35,7 @@ class SuperAndroidTextField extends StatefulWidget {
this.textController,
this.textAlign = TextAlign.left,
this.textStyleBuilder = defaultTextFieldStyleBuilder,
this.inlineWidgetBuilders = const [],
this.hintBehavior = HintBehavior.displayHintUntilFocus,
this.hintBuilder,
this.minLines,
Expand Down Expand Up @@ -69,6 +71,9 @@ class SuperAndroidTextField extends StatefulWidget {
/// [textController] based on the attributions in that content.
final AttributionStyleBuilder textStyleBuilder;

/// {@macro super_text_field_inline_widget_builders}
final InlineWidgetBuilderChain inlineWidgetBuilders;

/// Policy for when the hint should be displayed.
final HintBehavior hintBehavior;

Expand Down Expand Up @@ -606,7 +611,7 @@ class SuperAndroidTextFieldState extends State<SuperAndroidTextField>

Widget _buildSelectableText() {
final textSpan = _textEditingController.text.isNotEmpty
? _textEditingController.text.computeTextSpan(widget.textStyleBuilder)
? _textEditingController.text.computeInlineSpan(context, widget.textStyleBuilder, widget.inlineWidgetBuilders)
: TextSpan(text: "", style: widget.textStyleBuilder({}));

return SuperText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SuperDesktopTextField extends StatefulWidget {
this.tapRegionGroupId,
this.textController,
this.textStyleBuilder = defaultTextFieldStyleBuilder,
this.inlineWidgetBuilders = const [],
this.textAlign = TextAlign.left,
this.hintBehavior = HintBehavior.displayHintUntilFocus,
this.hintBuilder,
Expand Down Expand Up @@ -93,6 +94,9 @@ class SuperDesktopTextField extends StatefulWidget {
/// [textController] based on the attributions in that content.
final AttributionStyleBuilder textStyleBuilder;

/// {@macro super_text_field_inline_widget_builders}
final InlineWidgetBuilderChain inlineWidgetBuilders;

/// Policy for when the hint should be displayed.
final HintBehavior hintBehavior;

Expand Down Expand Up @@ -497,7 +501,7 @@ class SuperDesktopTextFieldState extends State<SuperDesktopTextField> implements
Widget _buildSelectableText() {
return SuperText(
key: _textKey,
richText: _controller.text.computeTextSpan(widget.textStyleBuilder),
richText: _controller.text.computeInlineSpan(context, widget.textStyleBuilder, widget.inlineWidgetBuilders),
textAlign: widget.textAlign,
textScaler: _textScaler,
layerBeneathBuilder: (context, textLayout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ class AttributedTextEditingController with ChangeNotifier {
notifyListeners();
}

@Deprecated('Use text.computeInlineSpan() instead, which adds support for inline widgets.')
TextSpan buildTextSpan(AttributionStyleBuilder styleBuilder) {
return text.computeTextSpan(styleBuilder);
}
Expand Down
9 changes: 6 additions & 3 deletions super_editor/lib/src/super_textfield/ios/ios_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SuperIOSTextField extends StatefulWidget {
this.tapHandlers = const [],
this.textController,
this.textStyleBuilder = defaultTextFieldStyleBuilder,
this.inlineWidgetBuilders = const [],
this.textAlign = TextAlign.left,
this.padding,
this.hintBehavior = HintBehavior.displayHintUntilFocus,
Expand Down Expand Up @@ -79,6 +80,9 @@ class SuperIOSTextField extends StatefulWidget {
/// [textController] based on the attributions in that content.
final AttributionStyleBuilder textStyleBuilder;

/// {@macro super_text_field_inline_widget_builders}
final InlineWidgetBuilderChain inlineWidgetBuilders;

/// Padding placed around the text content of this text field, but within the
/// scrollable viewport.
final EdgeInsets? padding;
Expand Down Expand Up @@ -604,9 +608,8 @@ class SuperIOSTextFieldState extends State<SuperIOSTextField>
}

Widget _buildSelectableText() {
final textSpan = _textEditingController.text.isNotEmpty
? _textEditingController.text.computeTextSpan(widget.textStyleBuilder)
: AttributedText().computeTextSpan(widget.textStyleBuilder);
final textSpan = _textEditingController.text //
.computeInlineSpan(context, widget.textStyleBuilder, widget.inlineWidgetBuilders);

CaretStyle caretStyle = widget.caretStyle;

Expand Down
12 changes: 12 additions & 0 deletions super_editor/lib/src/super_textfield/super_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SuperTextField extends StatefulWidget {
this.textController,
this.textAlign = TextAlign.left,
this.textStyleBuilder = defaultTextFieldStyleBuilder,
this.inlineWidgetBuilders = const [],
this.hintBehavior = HintBehavior.displayHintUntilFocus,
this.hintBuilder,
this.controlsColor,
Expand Down Expand Up @@ -104,6 +105,14 @@ class SuperTextField extends StatefulWidget {
/// [textController] based on the attributions in that content.
final AttributionStyleBuilder textStyleBuilder;

/// {@template super_text_field_inline_widget_builders}
/// A Chain of Responsibility that's used to build inline widgets.
///
/// The first builder in the chain to return a non-null `Widget` will be
/// used for a given inline placeholder.
/// {@endtemplate}
final InlineWidgetBuilderChain inlineWidgetBuilders;

/// Policy for when the hint should be displayed.
final HintBehavior hintBehavior;

Expand Down Expand Up @@ -361,6 +370,7 @@ class SuperTextFieldState extends State<SuperTextField> implements ImeInputOwner
textController: _controller,
textAlign: widget.textAlign,
textStyleBuilder: widget.textStyleBuilder,
inlineWidgetBuilders: widget.inlineWidgetBuilders,
hintBehavior: widget.hintBehavior,
hintBuilder: widget.hintBuilder,
selectionHighlightStyle: SelectionHighlightStyle(
Expand Down Expand Up @@ -395,6 +405,7 @@ class SuperTextFieldState extends State<SuperTextField> implements ImeInputOwner
textController: _controller,
textAlign: widget.textAlign,
textStyleBuilder: widget.textStyleBuilder,
inlineWidgetBuilders: widget.inlineWidgetBuilders,
hintBehavior: widget.hintBehavior,
hintBuilder: widget.hintBuilder,
caretStyle: widget.caretStyle ??
Expand Down Expand Up @@ -424,6 +435,7 @@ class SuperTextFieldState extends State<SuperTextField> implements ImeInputOwner
textController: _controller,
textAlign: widget.textAlign,
textStyleBuilder: widget.textStyleBuilder,
inlineWidgetBuilders: widget.inlineWidgetBuilders,
padding: widget.padding,
hintBehavior: widget.hintBehavior,
hintBuilder: widget.hintBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ void main() {
);

// Ensure the text is colored orange.
expect(
SuperTextFieldInspector.findRichText().style!.color,
Colors.orange,
);
for (int i = 0; i <= 9; i++) {
expect(
SuperTextFieldInspector.findRichText().getSpanForPosition(TextPosition(offset: i))!.style!.color,
Colors.orange,
);
}
});

testWidgetsOnAllPlatforms("to partial text", (tester) async {
Expand Down
Loading
Loading