Flutter 带颤振的局部超驰CupertinoTheme

Flutter 带颤振的局部超驰CupertinoTheme,flutter,flutter-cupertino,Flutter,Flutter Cupertino,我正在使用Cupertino小部件,需要本地覆盖我的全局CupertinoTheme,并为此使用CupertinoTheme小部件。我的用例是在图像顶部显示文本时强制使用一些“黑色”主题,但问题是一般性的 在下面的示例中,我尝试更改一种文本样式的字体大小(从42px更改为21px),但没有应用:两种文本的大小相同(第二种文本的大小应为21px) 似乎CupertinoTheme.of(context)不读取重写样式,这与 子代小部件可以通过调用CupertinoTheme.of 这是一个样本(可

我正在使用Cupertino小部件,需要本地覆盖我的全局CupertinoTheme,并为此使用
CupertinoTheme
小部件。我的用例是在图像顶部显示文本时强制使用一些“黑色”主题,但问题是一般性的

在下面的示例中,我尝试更改一种文本样式的字体大小(从42px更改为21px),但没有应用:两种文本的大小相同(第二种文本的大小应为21px)

似乎
CupertinoTheme.of(context)
不读取重写样式,这与

子代小部件可以通过调用CupertinoTheme.of

这是一个样本(可以在上面测试):


你从错误的上下文中得到了主题。上下文必须是CupertinoTheme小部件(或者更确切地说是将从中创建的元素)的后代。尝试:

通过构建方法的content参数,您可以访问构建方法小部件的祖先所做的任何事情。无论您在构建方法中做什么,都不会对其产生影响


小部件是创建元素树的方法。在build(er)方法中获得的上下文参数是为该小部件创建的元素(简化的接口)。of(context)方法通常搜索context的祖先元素以找到一个Foo。(在某些情况下,存在缓存,因此它不是一个缓慢的搜索。)当您在构建方法中创建小部件树时,您只是在创建小部件;元素将在构建方法竞争之后创建。使用构建器小部件,就像我在上面所做的那样,延迟在构建器的构建器参数中创建小部件,直到为构建器(及其上方的小部件)创建元素之后。所以这是一种解决问题的方法。另一种方法是使用代码中CupertinoTheme的子部件创建一个新的无状态部件,因为它同样会延迟这些部件的创建,直到创建该无状态部件(及其父部件)的元素之后。

感谢@spersten,您的回答:这解决了我的问题,让我在颤振学习上取得进步。
import 'package:flutter/cupertino.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: true,
      theme: CupertinoThemeData(
        brightness: Brightness.dark,
        textTheme: CupertinoTextThemeData(
          navLargeTitleTextStyle: TextStyle(fontSize: 42)
        )
      ),
      home: Home()
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Column(
        children: [
           Text(
             'Hello, World #1!',
             style: CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle
            ),
            CupertinoTheme(
              data: CupertinoThemeData(
                textTheme: CupertinoTextThemeData(
                  navLargeTitleTextStyle: TextStyle(fontSize: 21)
                )
              ),
              child: Text(
                'Hello, World #2!',
                style:
                CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle
              ),
            ),
        ]
      )
    );
  }
}
CupertinoTheme(
  data: ...,
  child: Builder(
    builder: (context) => ... CupertinoTheme.of(contex)...
  )
)