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] Fix horizontal aligment (Resolves #668) #716

Merged
merged 13 commits into from
Aug 14, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:super_editor/src/infrastructure/attributed_text_styles.dart';
import 'package:super_editor/src/infrastructure/focus.dart';
import 'package:super_editor/src/infrastructure/super_textfield/android/_editing_controls.dart';
import 'package:super_editor/src/infrastructure/super_textfield/android/_user_interaction.dart';
import 'package:super_editor/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart';
import 'package:super_editor/src/infrastructure/super_textfield/infrastructure/hint_text.dart';
import 'package:super_editor/src/infrastructure/super_textfield/infrastructure/text_scrollview.dart';
import 'package:super_editor/src/infrastructure/super_textfield/input_method_engine/_ime_text_editing_controller.dart';
Expand Down Expand Up @@ -472,6 +473,7 @@ class SuperAndroidTextFieldState extends State<SuperAndroidTextField>
textScrollController: _textScrollController,
textKey: _textContentKey,
textEditingController: _textEditingController,
textAlign: widget.textAlign,
minLines: widget.minLines,
maxLines: widget.maxLines,
lineHeight: widget.lineHeight,
Expand Down Expand Up @@ -509,19 +511,21 @@ class SuperAndroidTextFieldState extends State<SuperAndroidTextField>
? _textEditingController.text.computeTextSpan(widget.textStyleBuilder)
: TextSpan(text: "", style: widget.textStyleBuilder({}));

return SuperTextWithSelection.single(
key: _textContentKey,
richText: textSpan,
textAlign: widget.textAlign,
userSelection: UserSelection(
highlightStyle: SelectionHighlightStyle(
color: widget.selectionColor,
),
caretStyle: CaretStyle(
color: widget.caretColor,
return FillWidthIfConstrained(
child: SuperTextWithSelection.single(
key: _textContentKey,
richText: textSpan,
textAlign: widget.textAlign,
userSelection: UserSelection(
highlightStyle: SelectionHighlightStyle(
color: widget.selectionColor,
),
caretStyle: CaretStyle(
color: widget.caretColor,
),
selection: _textEditingController.selection,
hasCaret: _focusNode.hasFocus,
),
selection: _textEditingController.selection,
hasCaret: _focusNode.hasFocus,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:super_text_layout/super_text_layout.dart';

import '../../keyboard.dart';
import '../../multi_tap_gesture.dart';
import '../infrastructure/fill_width_if_constrained.dart';
import '../styles.dart';

final _log = textFieldLog;
Expand Down Expand Up @@ -297,6 +298,7 @@ class SuperDesktopTextFieldState extends State<SuperDesktopTextField> implements
key: _textScrollKey,
textKey: _textKey,
textController: _controller,
textAlign: widget.textAlign,
scrollController: _scrollController,
viewportHeight: _viewportHeight,
estimatedLineHeight: _getEstimatedLineHeight(),
Expand All @@ -323,15 +325,17 @@ class SuperDesktopTextFieldState extends State<SuperDesktopTextField> implements
}

Widget _buildSelectableText() {
return SuperTextWithSelection.single(
key: _textKey,
richText: _controller.text.computeTextSpan(widget.textStyleBuilder),
textAlign: widget.textAlign,
userSelection: UserSelection(
highlightStyle: widget.selectionHighlightStyle,
caretStyle: widget.caretStyle,
selection: _controller.selection,
hasCaret: _focusNode.hasFocus,
return FillWidthIfConstrained(
child: SuperTextWithSelection.single(
key: _textKey,
richText: _controller.text.computeTextSpan(widget.textStyleBuilder),
textAlign: widget.textAlign,
userSelection: UserSelection(
highlightStyle: widget.selectionHighlightStyle,
caretStyle: widget.caretStyle,
selection: _controller.selection,
hasCaret: _focusNode.hasFocus,
),
),
);
}
Expand Down Expand Up @@ -866,6 +870,7 @@ class SuperTextFieldScrollview extends StatefulWidget {
required this.viewportHeight,
required this.estimatedLineHeight,
required this.isMultiline,
this.textAlign = TextAlign.left,
required this.child,
}) : super(key: key);

Expand Down Expand Up @@ -895,6 +900,9 @@ class SuperTextFieldScrollview extends StatefulWidget {
/// Whether or not this text field allows multiple lines of text.
final bool isMultiline;

/// The text alignment within the scrollview.
final TextAlign textAlign;

/// The rest of the subtree for this text field.
final Widget child;

Expand Down Expand Up @@ -1123,6 +1131,22 @@ class SuperTextFieldScrollviewState extends State<SuperTextFieldScrollview> with
}
}

Alignment _getAlignment() {
switch (widget.textAlign) {
case TextAlign.left:
case TextAlign.justify:
return Alignment.topLeft;
case TextAlign.right:
return Alignment.topRight;
case TextAlign.center:
return Alignment.topCenter;
case TextAlign.start:
return Directionality.of(context) == TextDirection.ltr ? Alignment.topLeft : Alignment.topRight;
case TextAlign.end:
return Directionality.of(context) == TextDirection.ltr ? Alignment.topRight : Alignment.topLeft;
}
}

@override
Widget build(BuildContext context) {
return SizedBox(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

/// Forces [child] to take up all available width when the
/// incoming width constraint is bounded, otherwise the [child]
/// is sized by its intrinsic width.
///
/// This widget is used to correctly align the text of a multiline
/// [SuperTextWithSelection] with a constrained width.
class FillWidthIfConstrained extends SingleChildRenderObjectWidget {
const FillWidthIfConstrained({
required Widget child,
}) : super(child: child);

@override
RenderObject createRenderObject(BuildContext context) {
return RenderFillWidthIfConstrained(
minWidth: _getViewportWidth(context),
);
}

@override
void updateRenderObject(BuildContext context, RenderFillWidthIfConstrained renderObject) {
renderObject.minWidth = _getViewportWidth(context);
}

double? _getViewportWidth(BuildContext context) {
final scrollable = Scrollable.of(context);
if (scrollable == null) {
return null;
}

final direction = scrollable.axisDirection;
// We only need to specify the width if we are inside a horizontal scrollable,
// because in this case we might have an infinity maxWidth.
if (direction == AxisDirection.up || direction == AxisDirection.down) {
return null;
}
return (scrollable.context.findRenderObject() as RenderBox?)?.size.width;
}
}

class RenderFillWidthIfConstrained extends RenderProxyBox {
RenderFillWidthIfConstrained({
double? minWidth,
}) : _minWidth = minWidth;

/// Sets the minimum width the child widget needs to be.
///
/// This is needed when this widget is inside a horizontal Scrollable.
/// In this case, we might have an infinity maxWidth, so we need
/// to specify the Scrollable's width to force the child to
/// be at least this width.
set minWidth(double? value) {
_minWidth = value;
markNeedsLayout();
}

double? _minWidth;

@override
void performLayout() {
BoxConstraints childConstraints = constraints;

// If the available width is bounded,
// force the child to be as wide as the available width.
if (constraints.hasBoundedWidth) {
childConstraints = BoxConstraints(
minWidth: constraints.maxWidth,
minHeight: constraints.minHeight,
maxWidth: constraints.maxWidth,
maxHeight: constraints.maxHeight,
);
} else if (_minWidth != null) {
// If a minWidth is given, force the child to be at least this width.
// This is the case when this widget is placed inside an Scrollable.
childConstraints = BoxConstraints(
minWidth: _minWidth!,
minHeight: constraints.minHeight,
maxHeight: constraints.maxHeight,
);
}

child!.layout(childConstraints, parentUsesSize: true);
size = child!.size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TextScrollView extends StatefulWidget {
this.lineHeight,
this.perLineAutoScrollDuration = Duration.zero,
this.showDebugPaint = false,
this.textAlign = TextAlign.left,
required this.child,
}) : super(key: key);

Expand Down Expand Up @@ -89,6 +90,9 @@ class TextScrollView extends StatefulWidget {
/// Whether to paint debug guides.
final bool showDebugPaint;

/// The text alignment within the scrollview.
final TextAlign textAlign;

/// The child widget.
final Widget child;

Expand Down Expand Up @@ -451,15 +455,34 @@ class _TextScrollViewState extends State<TextScrollView>
);
}

Alignment _getAlignment() {
switch (widget.textAlign) {
case TextAlign.left:
case TextAlign.justify:
return Alignment.topLeft;
case TextAlign.right:
return Alignment.topRight;
case TextAlign.center:
return Alignment.topCenter;
case TextAlign.start:
return Directionality.of(context) == TextDirection.ltr ? Alignment.topLeft : Alignment.topRight;
case TextAlign.end:
return Directionality.of(context) == TextDirection.ltr ? Alignment.topRight : Alignment.topLeft;
}
}

Widget _buildScrollView({
required Widget child,
}) {
return SingleChildScrollView(
key: _textFieldViewportKey,
controller: _scrollController,
physics: const NeverScrollableScrollPhysics(),
scrollDirection: isMultiline ? Axis.vertical : Axis.horizontal,
child: widget.child,
return Align(
alignment: _getAlignment(),
child: SingleChildScrollView(
key: _textFieldViewportKey,
controller: _scrollController,
physics: const NeverScrollableScrollPhysics(),
scrollDirection: isMultiline ? Axis.vertical : Axis.horizontal,
child: widget.child,
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:super_editor/src/infrastructure/_listenable_builder.dart';
import 'package:super_editor/src/infrastructure/_logging.dart';
import 'package:super_editor/src/infrastructure/attributed_text_styles.dart';
import 'package:super_editor/src/infrastructure/focus.dart';
import 'package:super_editor/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart';
import 'package:super_editor/src/infrastructure/super_textfield/infrastructure/hint_text.dart';
import 'package:super_editor/src/infrastructure/super_textfield/infrastructure/text_scrollview.dart';
import 'package:super_editor/src/infrastructure/super_textfield/input_method_engine/_ime_text_editing_controller.dart';
Expand Down Expand Up @@ -470,6 +471,7 @@ class SuperIOSTextFieldState extends State<SuperIOSTextField>
textScrollController: _textScrollController,
textKey: _textContentKey,
textEditingController: _textEditingController,
textAlign: widget.textAlign,
minLines: widget.minLines,
maxLines: widget.maxLines,
lineHeight: widget.lineHeight,
Expand Down Expand Up @@ -516,19 +518,21 @@ class SuperIOSTextFieldState extends State<SuperIOSTextField>
? _textEditingController.text.computeTextSpan(widget.textStyleBuilder)
: AttributedText(text: "").computeTextSpan(widget.textStyleBuilder);

return SuperTextWithSelection.single(
key: _textContentKey,
richText: textSpan,
textAlign: widget.textAlign,
userSelection: UserSelection(
highlightStyle: SelectionHighlightStyle(
color: widget.selectionColor,
),
caretStyle: CaretStyle(
color: _floatingCursorController.isShowingFloatingCursor ? Colors.grey : widget.caretColor,
return FillWidthIfConstrained(
child: SuperTextWithSelection.single(
key: _textContentKey,
richText: textSpan,
textAlign: widget.textAlign,
userSelection: UserSelection(
highlightStyle: SelectionHighlightStyle(
color: widget.selectionColor,
),
caretStyle: CaretStyle(
color: _floatingCursorController.isShowingFloatingCursor ? Colors.grey : widget.caretColor,
),
selection: _textEditingController.selection,
hasCaret: _focusNode.hasFocus,
),
selection: _textEditingController.selection,
hasCaret: _focusNode.hasFocus,
),
);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading