diff --git a/super_editor/lib/src/infrastructure/super_textfield/desktop/desktop_textfield.dart b/super_editor/lib/src/infrastructure/super_textfield/desktop/desktop_textfield.dart index 570545ca64..b62809ad57 100644 --- a/super_editor/lib/src/infrastructure/super_textfield/desktop/desktop_textfield.dart +++ b/super_editor/lib/src/infrastructure/super_textfield/desktop/desktop_textfield.dart @@ -1149,35 +1149,19 @@ class SuperTextFieldScrollviewState extends State with @override Widget build(BuildContext context) { - return _buildAlign( - SizedBox( - height: widget.viewportHeight, - child: SingleChildScrollView( - controller: widget.scrollController, - physics: const NeverScrollableScrollPhysics(), - scrollDirection: widget.isMultiline ? Axis.vertical : Axis.horizontal, - child: Padding( - padding: widget.padding, - child: widget.child, - ), + return SizedBox( + height: widget.viewportHeight, + child: SingleChildScrollView( + controller: widget.scrollController, + physics: const NeverScrollableScrollPhysics(), + scrollDirection: widget.isMultiline ? Axis.vertical : Axis.horizontal, + child: Padding( + padding: widget.padding, + child: widget.child, ), ), ); } - - Widget _buildAlign(Widget child) { - // Multiline textfields are already aligned correctly - // because they have a maxWidth constraint. - if (widget.isMultiline) { - return child; - } - // Singleline textfields have an infinity maxWidth - // so we need to align the whole scrollview - return Align( - alignment: _getAlignment(), - child: child, - ); - } } typedef RightClickListener = void Function( diff --git a/super_editor/lib/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart b/super_editor/lib/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart index ee35d6a026..10fcd23ee9 100644 --- a/super_editor/lib/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart +++ b/super_editor/lib/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart @@ -14,23 +14,43 @@ class FillWidthIfConstrained extends SingleChildRenderObjectWidget { @override RenderObject createRenderObject(BuildContext context) { - return _RenderFillWidthIfConstrained(); + return RenderFillWidthIfConstrained( + viewportWidth: _getViewportWidth(context), + ); } @override - void updateRenderObject(BuildContext context, RenderObject renderObject) { - renderObject.markNeedsLayout(); + void updateRenderObject(BuildContext context, RenderFillWidthIfConstrained renderObject) { + renderObject.viewportWidth = _getViewportWidth(context); + } + + double? _getViewportWidth(BuildContext context) { + final scrollable = Scrollable.of(context); + if (scrollable == null) { + return null; + } + return (scrollable.context.findRenderObject() as RenderBox?)?.size.width; } } -class _RenderFillWidthIfConstrained extends RenderProxyBox { +class RenderFillWidthIfConstrained extends RenderProxyBox { + RenderFillWidthIfConstrained({ + double? viewportWidth, + }) : _viewportWidth = viewportWidth; + + set viewportWidth(double? value) { + _viewportWidth = value; + markNeedsLayout(); + } + + double? _viewportWidth = 0; + @override void performLayout() { BoxConstraints childConstraints = constraints; - // If the available width is bounded and the child did not - // take all available width, force the child to be as wide - // as the available width. + // 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, @@ -38,9 +58,17 @@ class _RenderFillWidthIfConstrained extends RenderProxyBox { maxWidth: constraints.maxWidth, maxHeight: constraints.maxHeight, ); + } else if (_viewportWidth != null) { + // If a viewport width 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: _viewportWidth!, + minHeight: constraints.minHeight, + maxHeight: constraints.maxHeight, + ); } child!.layout(childConstraints, parentUsesSize: true); size = child!.size; - } + } } diff --git a/super_editor/test/super_textfield/super_textfield_text_alignment_test.dart b/super_editor/test/super_textfield/super_textfield_text_alignment_test.dart index 1d69aa6119..6ae8b5e13f 100644 --- a/super_editor/test/super_textfield/super_textfield_text_alignment_test.dart +++ b/super_editor/test/super_textfield/super_textfield_text_alignment_test.dart @@ -220,8 +220,8 @@ Widget _buildSuperTextField({ text: AttributedText(text: text), ); - return ConstrainedBox( - constraints: const BoxConstraints(minWidth: 300), + return SizedBox( + width: double.infinity, child: SuperTextField( configuration: configuration, textController: controller,