mirror of
https://github.com/flutter/flutter
synced 2024-08-27 12:00:58 +00:00
Fix SegmentedButton
states update logic (#140772)
This PR fixes: #140746 Desc: Style state update logic fix on SegmentedButton
This commit is contained in:
parent
9ad475858f
commit
2ae39c0cc0
|
@ -472,9 +472,7 @@ class SegmentedButtonState<T> extends State<SegmentedButton<T>> {
|
|||
? segment.icon
|
||||
: null;
|
||||
final MaterialStatesController controller = statesControllers.putIfAbsent(segment, () => MaterialStatesController());
|
||||
controller.value = <MaterialState>{
|
||||
if (segmentSelected) MaterialState.selected,
|
||||
};
|
||||
controller.update(MaterialState.selected, segmentSelected);
|
||||
|
||||
final Widget button = icon != null
|
||||
? TextButton.icon(
|
||||
|
|
|
@ -232,7 +232,7 @@ void main() {
|
|||
expect(selection, <int>{2, 3});
|
||||
});
|
||||
|
||||
testWidgets('SegmentedButton allows for empty selection', (WidgetTester tester) async {
|
||||
testWidgets('SegmentedButton allows for empty selection', (WidgetTester tester) async {
|
||||
int callbackCount = 0;
|
||||
int? selectedSegment = 1;
|
||||
|
||||
|
@ -285,7 +285,7 @@ testWidgets('SegmentedButton allows for empty selection', (WidgetTester tester)
|
|||
expect(selectedSegment, 3);
|
||||
});
|
||||
|
||||
testWidgets('SegmentedButton shows checkboxes for selected segments', (WidgetTester tester) async {
|
||||
testWidgets('SegmentedButton shows checkboxes for selected segments', (WidgetTester tester) async {
|
||||
Widget frameWithSelection(int selected) {
|
||||
return Material(
|
||||
child: boilerplate(
|
||||
|
@ -646,10 +646,10 @@ testWidgets('SegmentedButton shows checkboxes for selected segments', (WidgetTes
|
|||
ButtonSegment<int>(value: 1, label: Text('1')),
|
||||
ButtonSegment<int>(value: 2, label: Text('2'), tooltip: 't2'),
|
||||
ButtonSegment<int>(
|
||||
value: 3,
|
||||
label: Text('3'),
|
||||
tooltip: 't3',
|
||||
enabled: false,
|
||||
value: 3,
|
||||
label: Text('3'),
|
||||
tooltip: 't3',
|
||||
enabled: false,
|
||||
),
|
||||
],
|
||||
selected: const <int>{2},
|
||||
|
@ -774,6 +774,45 @@ testWidgets('SegmentedButton shows checkboxes for selected segments', (WidgetTes
|
|||
await tester.pumpAndSettle();
|
||||
expect(overlayColor(), paints..rect(color: foregroundColor.withOpacity(0.08)));
|
||||
});
|
||||
|
||||
testWidgets('Disabled SegmentedButton has correct states when rebuilding', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: StatefulBuilder(
|
||||
builder: (BuildContext context, StateSetter setState) {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
SegmentedButton<int>(
|
||||
segments: const <ButtonSegment<int>>[
|
||||
ButtonSegment<int>(value: 0, label: Text('foo')),
|
||||
],
|
||||
selected: const <int>{0},
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => setState(() {}),
|
||||
child: const Text('Trigger rebuild'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
final Set<MaterialState> states = <MaterialState>{ MaterialState.selected, MaterialState.disabled };
|
||||
// Check the initial states.
|
||||
SegmentedButtonState<int> state = tester.state(find.byType(SegmentedButton<int>));
|
||||
expect(state.statesControllers.values.first.value, states);
|
||||
// Trigger a rebuild.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pumpAndSettle();
|
||||
// Check the states after the rebuild.
|
||||
state = tester.state(find.byType(SegmentedButton<int>));
|
||||
expect(state.statesControllers.values.first.value, states);
|
||||
});
|
||||
}
|
||||
|
||||
Set<MaterialState> enabled = const <MaterialState>{};
|
||||
|
|
Loading…
Reference in a new issue