Skip to content

Commit ca33836

Browse files
Fix context menu button color on Android when textButtonTheme is set (#133271)
Fixes #133027 When setting a `textButtonTheme` it should not override the native context menu colors. ```dart theme: ThemeData( textButtonTheme: const TextButtonThemeData( style: ButtonStyle( backgroundColor: MaterialStatePropertyAll<Color>( Color(0xff05164d)), // blue color ), ), ), ``` Before|After --|-- <img width="341" alt="Screenshot 2023-08-24 at 1 17 25 PM" src="https://github.com/flutter/flutter/assets/948037/30ea0ef8-b41a-4e1f-93a3-50fcd87ab2bf">|<img width="341" alt="Screenshot 2023-08-24 at 1 15 35 PM" src="https://github.com/flutter/flutter/assets/948037/5f59481c-aa5d-4850-aa4b-daa678e54044">
1 parent c046627 commit ca33836

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
135135
static const Color _defaultForegroundColorLight = Color(0xff000000);
136136
static const Color _defaultForegroundColorDark = Color(0xffffffff);
137137

138+
// The background color is hardcoded to transparent by default so the buttons
139+
// are the color of the container behind them. For example TextSelectionToolbar
140+
// hardcodes the color value, and TextSelectionToolbarTextButtons that are its
141+
// children become that color.
142+
static const Color _defaultBackgroundColorTransparent = Color(0x00000000);
143+
138144
static Color _getForegroundColor(ColorScheme colorScheme) {
139145
final bool isDefaultOnSurface = switch (colorScheme.brightness) {
140146
Brightness.light => identical(ThemeData().colorScheme.onSurface, colorScheme.onSurface),
@@ -154,6 +160,7 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
154160
final ColorScheme colorScheme = Theme.of(context).colorScheme;
155161
return TextButton(
156162
style: TextButton.styleFrom(
163+
backgroundColor: _defaultBackgroundColorTransparent,
157164
foregroundColor: _getForegroundColor(colorScheme),
158165
shape: const RoundedRectangleBorder(),
159166
minimumSize: const Size(kMinInteractiveDimension, kMinInteractiveDimension),

packages/flutter/test/material/text_selection_toolbar_text_button_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,72 @@ void main() {
123123
customForegroundColor,
124124
);
125125
});
126+
127+
testWidgetsWithLeakTracking('background color by default', (WidgetTester tester) async {
128+
// Regression test for https://github.com/flutter/flutter/issues/133027
129+
await tester.pumpWidget(
130+
MaterialApp(
131+
theme: ThemeData(
132+
colorScheme: colorScheme,
133+
),
134+
home: Scaffold(
135+
body: Center(
136+
child: TextSelectionToolbarTextButton(
137+
padding: TextSelectionToolbarTextButton.getPadding(0, 1),
138+
child: const Text('button'),
139+
),
140+
),
141+
),
142+
),
143+
);
144+
145+
expect(find.byType(TextButton), findsOneWidget);
146+
147+
final TextButton textButton = tester.widget(find.byType(TextButton));
148+
// The background color is hardcoded to transparent by default so the buttons
149+
// are the color of the container behind them. For example TextSelectionToolbar
150+
// hardcodes the color value, and TextSelectionToolbarTextButton that are its
151+
// children should be that color.
152+
expect(
153+
textButton.style!.backgroundColor!.resolve(<MaterialState>{}),
154+
Colors.transparent,
155+
);
156+
});
157+
158+
testWidgetsWithLeakTracking('textButtonTheme should not override default background color', (WidgetTester tester) async {
159+
// Regression test for https://github.com/flutter/flutter/issues/133027
160+
await tester.pumpWidget(
161+
MaterialApp(
162+
theme: ThemeData(
163+
colorScheme: colorScheme,
164+
textButtonTheme: const TextButtonThemeData(
165+
style: ButtonStyle(
166+
backgroundColor: MaterialStatePropertyAll<Color>(Colors.blue),
167+
),
168+
),
169+
),
170+
home: Scaffold(
171+
body: Center(
172+
child: TextSelectionToolbarTextButton(
173+
padding: TextSelectionToolbarTextButton.getPadding(0, 1),
174+
child: const Text('button'),
175+
),
176+
),
177+
),
178+
),
179+
);
180+
181+
expect(find.byType(TextButton), findsOneWidget);
182+
183+
final TextButton textButton = tester.widget(find.byType(TextButton));
184+
// The background color is hardcoded to transparent by default so the buttons
185+
// are the color of the container behind them. For example TextSelectionToolbar
186+
// hardcodes the color value, and TextSelectionToolbarTextButton that are its
187+
// children should be that color.
188+
expect(
189+
textButton.style!.backgroundColor!.resolve(<MaterialState>{}),
190+
Colors.transparent,
191+
);
192+
});
126193
}
127194
}

0 commit comments

Comments
 (0)