Theme(data: Theme.of(context).copyWith(xxx: ...), child: ...)テーマをオーバーライドしても、一部のウィジェットには影響しないことに気付きました。アプリ開発中に何度か同様の現象に遭遇しましたが、記憶にあるのは以下の1件のみです。
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: () => ...,
),
],
),
),
),
);
}
}
この例では、コンテキスト メニュー ( の長押しで表示) のテキストと背景の色もとTextFieldに変更する必要がありますが、実際にはボタン ラベルの色のみが変更されます。それはなぜです?私は何か間違ったことをしていますか?greengreen.shade100
