diff --git a/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart b/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart index e4ac077c14a..844da98ce05 100644 --- a/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart +++ b/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart @@ -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), diff --git a/packages/flutter/test/material/text_selection_toolbar_text_button_test.dart b/packages/flutter/test/material/text_selection_toolbar_text_button_test.dart index f2f6cbd10cf..24de64a8000 100644 --- a/packages/flutter/test/material/text_selection_toolbar_text_button_test.dart +++ b/packages/flutter/test/material/text_selection_toolbar_text_button_test.dart @@ -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({}), + 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(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({}), + Colors.transparent, + ); + }); } }