-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
574bc21
commit 6704f8e
Showing
12 changed files
with
435 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...itor/lib/src/infrastructure/super_textfield/infrastructure/fill_width_if_constrained.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+20.7 KB
...r/test/super_textfield/goldens/super_textfield_alignments_multiline_android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.7 KB
...r/test/super_textfield/goldens/super_textfield_alignments_multiline_desktop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.7 KB
...ditor/test/super_textfield/goldens/super_textfield_alignments_multiline_ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.5 KB
.../test/super_textfield/goldens/super_textfield_alignments_singleline_android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.5 KB
.../test/super_textfield/goldens/super_textfield_alignments_singleline_desktop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.5 KB
...itor/test/super_textfield/goldens/super_textfield_alignments_singleline_ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.