-
Notifications
You must be signed in to change notification settings - Fork 230
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
Changes from 12 commits
40feb6b
4cc547a
730c689
766a593
7a9bde6
8ddaa0f
7dace34
566a844
39cd336
47c7d94
8fd1540
2ee53e8
6f135c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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( | ||
viewportWidth: _getViewportWidth(context), | ||
); | ||
} | ||
|
||
@override | ||
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 { | ||
RenderFillWidthIfConstrained({ | ||
double? viewportWidth, | ||
}) : _viewportWidth = viewportWidth; | ||
|
||
set viewportWidth(double? value) { | ||
_viewportWidth = value; | ||
markNeedsLayout(); | ||
} | ||
|
||
double? _viewportWidth = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason that this property is given an initial value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This initialization isn't needed. I removed it. |
||
|
||
@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 (_viewportWidth != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we care whether the viewport scrolls vertically vs horizontally? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only care about horizontal scrollables. |
||
// 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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a dart doc that explains what viewport this is, and why we care.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also changed the property name to make it more clear.