1

我注意到覆盖主题Theme(data: Theme.of(context).copyWith(xxx: ...), child: ...)不会影响某些小部件。我在开发应用程序时多次遇到类似的现象,但以下是我记得的唯一例子。

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)也应该更改为greenand green.shade100,但实际上只有按钮标签颜色发生了变化。为什么?我做错什么了吗?

在此处输入图像描述

4

0 回答 0