Added customizable padding for the segmented controll (#34555)
This commit is contained in:
parent
a96c8e50ff
commit
60d2735e1a
@ -11,9 +11,9 @@ import 'package:flutter/widgets.dart';
|
|||||||
|
|
||||||
import 'theme.dart';
|
import 'theme.dart';
|
||||||
|
|
||||||
// Minimum padding from horizontal edges of segmented control to edges of
|
// Minimum padding from edges of the segmented control to edges of
|
||||||
// encompassing widget.
|
// encompassing widget.
|
||||||
const EdgeInsets _kHorizontalItemPadding = EdgeInsets.symmetric(horizontal: 16.0);
|
const EdgeInsetsGeometry _kHorizontalItemPadding = EdgeInsets.symmetric(horizontal: 16.0);
|
||||||
|
|
||||||
// Minimum height of the segmented control.
|
// Minimum height of the segmented control.
|
||||||
const double _kMinSegmentedControlHeight = 28.0;
|
const double _kMinSegmentedControlHeight = 28.0;
|
||||||
@ -87,6 +87,7 @@ class CupertinoSegmentedControl<T> extends StatefulWidget {
|
|||||||
this.selectedColor,
|
this.selectedColor,
|
||||||
this.borderColor,
|
this.borderColor,
|
||||||
this.pressedColor,
|
this.pressedColor,
|
||||||
|
this.padding,
|
||||||
}) : assert(children != null),
|
}) : assert(children != null),
|
||||||
assert(children.length >= 2),
|
assert(children.length >= 2),
|
||||||
assert(onValueChanged != null),
|
assert(onValueChanged != null),
|
||||||
@ -179,6 +180,11 @@ class CupertinoSegmentedControl<T> extends StatefulWidget {
|
|||||||
/// Defaults to the selectedColor at 20% opacity if null.
|
/// Defaults to the selectedColor at 20% opacity if null.
|
||||||
final Color pressedColor;
|
final Color pressedColor;
|
||||||
|
|
||||||
|
/// The CupertinoSegmentedControl will be placed inside this padding
|
||||||
|
///
|
||||||
|
/// Defaults to EdgeInsets.symmetric(horizontal: 16.0)
|
||||||
|
final EdgeInsetsGeometry padding;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_SegmentedControlState<T> createState() => _SegmentedControlState<T>();
|
_SegmentedControlState<T> createState() => _SegmentedControlState<T>();
|
||||||
}
|
}
|
||||||
@ -407,7 +413,7 @@ class _SegmentedControlState<T> extends State<CupertinoSegmentedControl<T>>
|
|||||||
);
|
);
|
||||||
|
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: _kHorizontalItemPadding.resolve(Directionality.of(context)),
|
padding: widget.padding ?? _kHorizontalItemPadding,
|
||||||
child: UnconstrainedBox(
|
child: UnconstrainedBox(
|
||||||
constrainedAxis: Axis.horizontal,
|
constrainedAxis: Axis.horizontal,
|
||||||
child: box,
|
child: box,
|
||||||
|
@ -117,6 +117,93 @@ void main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('Padding works', (WidgetTester tester) async {
|
||||||
|
const Key key = Key('Container');
|
||||||
|
|
||||||
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
|
children[0] = const SizedBox(
|
||||||
|
height: double.infinity,
|
||||||
|
child: Text('Child 1'),
|
||||||
|
) ;
|
||||||
|
children[1] = const SizedBox(
|
||||||
|
height: double.infinity,
|
||||||
|
child: Text('Child 2'),
|
||||||
|
) ;
|
||||||
|
|
||||||
|
Future<void> verifyPadding({ EdgeInsets padding }) async {
|
||||||
|
final EdgeInsets effectivePadding = padding ?? const EdgeInsets.symmetric(horizontal: 16);
|
||||||
|
final Rect segmentedControlRect = tester.getRect(find.byKey(key));
|
||||||
|
expect(
|
||||||
|
tester.getTopLeft(find.byWidget(children[0])),
|
||||||
|
segmentedControlRect.topLeft.translate(
|
||||||
|
effectivePadding.topLeft.dx,
|
||||||
|
effectivePadding.topLeft.dy,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
tester.getBottomLeft(find.byWidget(children[0])),
|
||||||
|
segmentedControlRect.bottomLeft.translate(
|
||||||
|
effectivePadding.bottomLeft.dx,
|
||||||
|
effectivePadding.bottomLeft.dy,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
tester.getTopRight(find.byWidget(children[1])),
|
||||||
|
segmentedControlRect.topRight.translate(
|
||||||
|
effectivePadding.topRight.dx,
|
||||||
|
effectivePadding.topRight.dy,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
tester.getBottomRight(find.byWidget(children[1])),
|
||||||
|
segmentedControlRect.bottomRight.translate(
|
||||||
|
effectivePadding.bottomRight.dx,
|
||||||
|
effectivePadding.bottomRight.dy,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
boilerplate(
|
||||||
|
child: CupertinoSegmentedControl<int>(
|
||||||
|
key: key,
|
||||||
|
children: children,
|
||||||
|
onValueChanged: (int newValue) { },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Default padding works.
|
||||||
|
await verifyPadding();
|
||||||
|
|
||||||
|
// Switch to Child 2 padding should remain the same.
|
||||||
|
await tester.tap(find.text('Child 2'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await verifyPadding();
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
boilerplate(
|
||||||
|
child: CupertinoSegmentedControl<int>(
|
||||||
|
key: key,
|
||||||
|
padding: const EdgeInsets.fromLTRB(1, 3, 5, 7),
|
||||||
|
children: children,
|
||||||
|
onValueChanged: (int newValue) { },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Custom padding works.
|
||||||
|
await verifyPadding(padding: const EdgeInsets.fromLTRB(1, 3, 5, 7));
|
||||||
|
|
||||||
|
// Switch back to Child 1 padding should remain the same.
|
||||||
|
await tester.tap(find.text('Child 1'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await verifyPadding(padding: const EdgeInsets.fromLTRB(1, 3, 5, 7));
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('Value attribute must be the key of one of the children widgets', (WidgetTester tester) async {
|
testWidgets('Value attribute must be the key of one of the children widgets', (WidgetTester tester) async {
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = const Text('Child 1');
|
children[0] = const Text('Child 1');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user