Unverified Commit ca33836b authored by Renzo Olivares's avatar Renzo Olivares Committed by GitHub

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">
parent c0466274
......@@ -135,6 +135,12 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
static const Color _defaultForegroundColorLight = Color(0xff000000);
static const Color _defaultForegroundColorDark = Color(0xffffffff);
// The background color is hardcoded to transparent by default so the buttons
// are the color of the container behind them. For example TextSelectionToolbar
// hardcodes the color value, and TextSelectionToolbarTextButtons that are its
// children become that color.
static const Color _defaultBackgroundColorTransparent = Color(0x00000000);
static Color _getForegroundColor(ColorScheme colorScheme) {
final bool isDefaultOnSurface = switch (colorScheme.brightness) {
Brightness.light => identical(ThemeData().colorScheme.onSurface, colorScheme.onSurface),
......@@ -154,6 +160,7 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
return TextButton(
style: TextButton.styleFrom(
backgroundColor: _defaultBackgroundColorTransparent,
foregroundColor: _getForegroundColor(colorScheme),
shape: const RoundedRectangleBorder(),
minimumSize: const Size(kMinInteractiveDimension, kMinInteractiveDimension),
......
......@@ -123,5 +123,72 @@ void main() {
customForegroundColor,
);
});
testWidgetsWithLeakTracking('background color by default', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/133027
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
colorScheme: colorScheme,
),
home: Scaffold(
body: Center(
child: TextSelectionToolbarTextButton(
padding: TextSelectionToolbarTextButton.getPadding(0, 1),
child: const Text('button'),
),
),
),
),
);
expect(find.byType(TextButton), findsOneWidget);
final TextButton textButton = tester.widget(find.byType(TextButton));
// The background color is hardcoded to transparent by default so the buttons
// are the color of the container behind them. For example TextSelectionToolbar
// hardcodes the color value, and TextSelectionToolbarTextButton that are its
// children should be that color.
expect(
textButton.style!.backgroundColor!.resolve(<MaterialState>{}),
Colors.transparent,
);
});
testWidgetsWithLeakTracking('textButtonTheme should not override default background color', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/133027
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
colorScheme: colorScheme,
textButtonTheme: const TextButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStatePropertyAll<Color>(Colors.blue),
),
),
),
home: Scaffold(
body: Center(
child: TextSelectionToolbarTextButton(
padding: TextSelectionToolbarTextButton.getPadding(0, 1),
child: const Text('button'),
),
),
),
),
);
expect(find.byType(TextButton), findsOneWidget);
final TextButton textButton = tester.widget(find.byType(TextButton));
// The background color is hardcoded to transparent by default so the buttons
// are the color of the container behind them. For example TextSelectionToolbar
// hardcodes the color value, and TextSelectionToolbarTextButton that are its
// children should be that color.
expect(
textButton.style!.backgroundColor!.resolve(<MaterialState>{}),
Colors.transparent,
);
});
}
}
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