Skip to content

Commit be5a842

Browse files
authored
FIX: SegmentedButton segments background cannot be made transparent (#123293)
1 parent 37d4e7d commit be5a842

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

packages/flutter/lib/src/material/segmented_button.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class SegmentedButton<T> extends StatelessWidget {
354354
final List<Widget> buttons = segments.map(buttonFor).toList();
355355

356356
return Material(
357+
type: MaterialType.transparency,
357358
shape: enabledBorder.copyWith(side: BorderSide.none),
358359
elevation: resolve<double?>((ButtonStyle? style) => style?.elevation)!,
359360
shadowColor: resolve<Color?>((ButtonStyle? style) => style?.shadowColor),

packages/flutter/test/material/segmented_button_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ Widget boilerplate({required Widget child}) {
1919

2020
void main() {
2121

22+
testWidgets('SegmentedButton is built with Material of type MaterialType.transparency', (WidgetTester tester) async {
23+
final ThemeData theme = ThemeData(useMaterial3: true);
24+
await tester.pumpWidget(
25+
MaterialApp(
26+
theme: theme,
27+
home: Scaffold(
28+
body: Center(
29+
child: SegmentedButton<int>(
30+
segments: const <ButtonSegment<int>>[
31+
ButtonSegment<int>(value: 1, label: Text('1')),
32+
ButtonSegment<int>(value: 2, label: Text('2')),
33+
ButtonSegment<int>(value: 3, label: Text('3'), enabled: false),
34+
],
35+
selected: const <int>{2},
36+
onSelectionChanged: (Set<int> selected) { },
37+
),
38+
),
39+
),
40+
),
41+
);
42+
43+
// Expect SegmentedButton to be built with type MaterialType.transparency.
44+
final Finder text = find.text('1');
45+
final Finder parent = find.ancestor(of: text, matching: find.byType(Material)).first;
46+
final Finder parentMaterial = find.ancestor(of: parent, matching: find.byType(Material)).first;
47+
final Material material = tester.widget<Material>(parentMaterial);
48+
expect(material.type, MaterialType.transparency);
49+
});
50+
2251
testWidgets('SegmentedButton supports exclusive choice by default', (WidgetTester tester) async {
2352
int callbackCount = 0;
2453
int selectedSegment = 2;

0 commit comments

Comments
 (0)