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">
This commit is contained in:
parent
c046627482
commit
ca33836b45
@ -135,6 +135,12 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
|
|||||||
static const Color _defaultForegroundColorLight = Color(0xff000000);
|
static const Color _defaultForegroundColorLight = Color(0xff000000);
|
||||||
static const Color _defaultForegroundColorDark = Color(0xffffffff);
|
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) {
|
static Color _getForegroundColor(ColorScheme colorScheme) {
|
||||||
final bool isDefaultOnSurface = switch (colorScheme.brightness) {
|
final bool isDefaultOnSurface = switch (colorScheme.brightness) {
|
||||||
Brightness.light => identical(ThemeData().colorScheme.onSurface, colorScheme.onSurface),
|
Brightness.light => identical(ThemeData().colorScheme.onSurface, colorScheme.onSurface),
|
||||||
@ -154,6 +160,7 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
|
|||||||
final ColorScheme colorScheme = Theme.of(context).colorScheme;
|
final ColorScheme colorScheme = Theme.of(context).colorScheme;
|
||||||
return TextButton(
|
return TextButton(
|
||||||
style: TextButton.styleFrom(
|
style: TextButton.styleFrom(
|
||||||
|
backgroundColor: _defaultBackgroundColorTransparent,
|
||||||
foregroundColor: _getForegroundColor(colorScheme),
|
foregroundColor: _getForegroundColor(colorScheme),
|
||||||
shape: const RoundedRectangleBorder(),
|
shape: const RoundedRectangleBorder(),
|
||||||
minimumSize: const Size(kMinInteractiveDimension, kMinInteractiveDimension),
|
minimumSize: const Size(kMinInteractiveDimension, kMinInteractiveDimension),
|
||||||
|
@ -123,5 +123,72 @@ void main() {
|
|||||||
customForegroundColor,
|
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,
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user