Unverified Commit 09c80aa4 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

Segmented control quick double tap fix (#44391)

parent d961ae85
......@@ -289,7 +289,8 @@ class CupertinoNavigationBar extends StatefulWidget implements ObstructingPrefer
/// {@endtemplate}
final Widget trailing;
// TODO(xster): implement support for double row navigation bars.
// TODO(xster): https://github.com/flutter/flutter/issues/10469 implement
// support for double row navigation bars.
/// {@template flutter.cupertino.navBar.backgroundColor}
/// The background color of the navigation bar. If it contains transparency, the
......
......@@ -331,10 +331,12 @@ class _SegmentedControlState<T> extends State<CupertinoSegmentedControl<T>>
}
void _onTap(T currentKey) {
if (currentKey != widget.groupValue && currentKey == _pressedKey) {
if (currentKey != _pressedKey)
return;
if (currentKey != widget.groupValue) {
widget.onValueChanged(currentKey);
_pressedKey = null;
}
_pressedKey = null;
}
Color getTextColor(int index, T currentKey) {
......@@ -472,7 +474,6 @@ class _RenderSegmentedControl<T> extends RenderBox
with ContainerRenderObjectMixin<RenderBox, ContainerBoxParentData<RenderBox>>,
RenderBoxContainerDefaultsMixin<RenderBox, ContainerBoxParentData<RenderBox>> {
_RenderSegmentedControl({
List<RenderBox> children,
@required int selectedIndex,
@required int pressedIndex,
@required TextDirection textDirection,
......@@ -483,9 +484,7 @@ class _RenderSegmentedControl<T> extends RenderBox
_selectedIndex = selectedIndex,
_pressedIndex = pressedIndex,
_backgroundColors = backgroundColors,
_borderColor = borderColor {
addAll(children);
}
_borderColor = borderColor;
int get selectedIndex => _selectedIndex;
int _selectedIndex;
......
......@@ -1384,6 +1384,47 @@ void main() {
expect(getBackgroundColor(tester, 1), isSameColorAs(CupertinoColors.white));
});
// Regression test: https://github.com/flutter/flutter/issues/43414.
testWidgets("Quick double tap doesn't break the internal state", (WidgetTester tester) async {
const Map<int, Widget> children = <int, Widget>{
0: Text('A'),
1: Text('B'),
2: Text('C'),
};
int sharedValue = 0;
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return boilerplate(
child: CupertinoSegmentedControl<int>(
key: const ValueKey<String>('Segmented Control'),
children: children,
onValueChanged: (int newValue) {
setState(() { sharedValue = newValue; });
},
groupValue: sharedValue,
),
);
},
),
);
await tester.tap(find.text('B'));
// sharedValue has been updated but widget.groupValue is not.
expect(sharedValue, 1);
// Land the second tap before the widget gets a chance to rebuild.
final TestGesture secondTap = await tester.startGesture(tester.getCenter(find.text('B')));
await tester.pump();
await secondTap.up();
expect(sharedValue, 1);
await tester.tap(find.text('C'));
expect(sharedValue, 2);
});
testWidgets('Golden Test Placeholder Widget', (WidgetTester tester) async {
final Map<int, Widget> children = <int, Widget>{};
children[0] = Container();
......
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