Skip to content

Commit

Permalink
Remove the usage of Align
Browse files Browse the repository at this point in the history
  • Loading branch information
angelosilvestre committed Aug 13, 2022
1 parent 8fd1540 commit 2ee53e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1149,35 +1149,19 @@ class SuperTextFieldScrollviewState extends State<SuperTextFieldScrollview> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,61 @@ 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,
minHeight: constraints.minHeight,
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2ee53e8

Please sign in to comment.