Unverified Commit b0046b18 authored by Natalie Sampsell's avatar Natalie Sampsell Committed by GitHub

Segmented control fixes (#20202)

Segment width now determined by width of widest child + children widgets now centered within segments
parent 99d5ef90
64b7a3a7aef2fea9c7529d4834bf9eb3d85602d8 46cf554baf4840c326bbceaa51b11534069bb557
\ No newline at end of file
...@@ -46,10 +46,11 @@ const Duration _kFadeDuration = Duration(milliseconds: 165); ...@@ -46,10 +46,11 @@ const Duration _kFadeDuration = Duration(milliseconds: 165);
/// The [children] will be displayed in the order of the keys in the [Map]. /// The [children] will be displayed in the order of the keys in the [Map].
/// The height of the segmented control is determined by the height of the /// The height of the segmented control is determined by the height of the
/// tallest widget provided as a value in the [Map] of [children]. /// tallest widget provided as a value in the [Map] of [children].
/// The width of the segmented control is determined by the horizontal /// The width of each child in the segmented control will be equal to the width
/// constraints on its parent. The available horizontal space is divided by /// of widest child, unless the combined width of the children is wider than
/// the number of provided [children] to determine the width of each widget. /// the available horizontal space. In this case, the available horizontal space
/// The selection area for each of the widgets in the [Map] of /// is divided by the number of provided [children] to determine the width of
/// each widget. The selection area for each of the widgets in the [Map] of
/// [children] will then be expanded to fill the calculated space, so each /// [children] will then be expanded to fill the calculated space, so each
/// widget will appear to have the same dimensions. /// widget will appear to have the same dimensions.
/// ///
...@@ -75,10 +76,10 @@ class SegmentedControl<T> extends StatefulWidget { ...@@ -75,10 +76,10 @@ class SegmentedControl<T> extends StatefulWidget {
/// in the [onValueChanged] callback when a new value from the [children] map /// in the [onValueChanged] callback when a new value from the [children] map
/// is selected. /// is selected.
/// ///
/// The [groupValue] must be one of the keys in the [children] map.
/// The [groupValue] is the currently selected value for the segmented control. /// The [groupValue] is the currently selected value for the segmented control.
/// If no [groupValue] is provided, or the [groupValue] is null, no widget will /// If no [groupValue] is provided, or the [groupValue] is null, no widget will
/// appear as selected. /// appear as selected. The [groupValue] must be either null or one of the keys
/// in the [children] map.
SegmentedControl({ SegmentedControl({
Key key, Key key,
@required this.children, @required this.children,
...@@ -91,7 +92,8 @@ class SegmentedControl<T> extends StatefulWidget { ...@@ -91,7 +92,8 @@ class SegmentedControl<T> extends StatefulWidget {
}) : assert(children != null), }) : assert(children != null),
assert(children.length >= 2), assert(children.length >= 2),
assert(onValueChanged != null), assert(onValueChanged != null),
assert(groupValue == null || children.keys.any((T child) => child == groupValue)), assert(groupValue == null || children.keys.any((T child) => child == groupValue),
'The groupValue must be either null or one of the keys in the children map.'),
assert(unselectedColor != null), assert(unselectedColor != null),
assert(selectedColor != null), assert(selectedColor != null),
assert(borderColor != null), assert(borderColor != null),
...@@ -189,7 +191,7 @@ class SegmentedControl<T> extends StatefulWidget { ...@@ -189,7 +191,7 @@ class SegmentedControl<T> extends StatefulWidget {
/// This attribute must not be null. /// This attribute must not be null.
/// ///
/// If this attribute is unspecified, this color will default to /// If this attribute is unspecified, this color will default to
/// 'Color(0x33007AFF)', a light, partially-transparent blue color. /// `Color(0x33007AFF)`, a light, partially-transparent blue color.
final Color pressedColor; final Color pressedColor;
@override @override
...@@ -346,7 +348,10 @@ class _SegmentedControlState<T> extends State<SegmentedControl<T>> ...@@ -346,7 +348,10 @@ class _SegmentedControlState<T> extends State<SegmentedControl<T>>
color: getTextColor(index, currentKey), color: getTextColor(index, currentKey),
); );
Widget child = widget.children[currentKey]; Widget child = new Center(
child: widget.children[currentKey],
);
child = new GestureDetector( child = new GestureDetector(
onTapDown: (TapDownDetails event) { onTapDown: (TapDownDetails event) {
_onTapDown(currentKey); _onTapDown(currentKey);
...@@ -599,15 +604,11 @@ class _RenderSegmentedControl<T> extends RenderBox ...@@ -599,15 +604,11 @@ class _RenderSegmentedControl<T> extends RenderBox
void performLayout() { void performLayout() {
double maxHeight = _kMinSegmentedControlHeight; double maxHeight = _kMinSegmentedControlHeight;
double childWidth; double childWidth = constraints.minWidth / childCount;
if (constraints.maxWidth.isFinite) { for (RenderBox child in getChildrenAsList()) {
childWidth = constraints.maxWidth / childCount; childWidth = math.max(childWidth, child.getMaxIntrinsicWidth(double.infinity));
} else {
childWidth = constraints.minWidth / childCount;
for (RenderBox child in getChildrenAsList()) {
childWidth = math.max(childWidth, child.getMaxIntrinsicWidth(double.infinity));
}
} }
childWidth = math.min(childWidth, constraints.maxWidth / childCount);
RenderBox child = firstChild; RenderBox child = firstChild;
while (child != null) { while (child != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment