I noticed that overriding the theme by Theme(data: Theme.of(context).copyWith(xxx: ...), child: ...)
does not affect some widgets.
I came across similar phenomena several times while I was developing an app, but the below is the only instance I remember.
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
canvasColor: Colors.blue.shade100, // Background color of TextField menu
buttonTheme: ThemeData().buttonTheme.copyWith(
textTheme: ButtonTextTheme.accent,
colorScheme: ColorScheme.light(secondary: Colors.blue), // Color of button label and of text on TextField menu
),
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.green.shade100, // This is ignored on TextField menu.
buttonTheme: Theme.of(context).buttonTheme.copyWith(
colorScheme: Theme.of(context)
.buttonTheme
.colorScheme
.copyWith(secondary: Colors.green), // This is applied to button label, but not to TextField menu.
),
),
child: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const TextField(),
RaisedButton(
child: const Text('Button'),
onPressed: () => ...,
),
],
),
),
),
);
}
}
In this example, the colors of the text and background of the context menu (shown on long-press of TextField
) should also be changed to green
and green.shade100
, but actually only the button label color is changed. Why is it? Am I doing anything wrong?