首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DefaultTextStyle在颤振中的应用

DefaultTextStyle在颤振中的应用
EN

Stack Overflow用户
提问于 2018-04-13 06:20:19
回答 2查看 12.1K关注 0票数 13

我正在尝试应用DefaultTextStyle,但即使样式已定义并可用(通过调用DefaultTextStyle.of(context).style建立),它在默认情况下也不会应用到子Text对象。那么,我做错了什么,或者没有理解呢?

下面是我的调用类中的build方法,我在其中定义了我的DefaultTextStyle

代码语言:javascript
运行
复制
  Widget build(BuildContext context) {
    return new MaterialApp(
      onGenerateTitle: (BuildContext context) =>
      Strings.of(context).getStr('app_title'),
      localizationsDelegates: [
        const StringsDelegate(),
        GlobalWidgetsLocalizations.delegate,
      ],
      localeResolutionCallback: Strings.resolveLocale,
      // Watch out: MaterialApp creates a Localizations widget
      // with the specified delegates. DemoLocalizations.of()
      // will only find the app's Localizations widget if its
      // context is a child of the app.
      theme: new ThemeData(
        primarySwatch: Colors.blue,
        ),
      home: new DefaultTextStyle(
        style: new TextStyle(
          fontWeight: FontWeight.bold,
          decoration: TextDecoration.underline,
          decorationColor: Colors.red,
          decorationStyle: TextDecorationStyle.wavy,
          color: Colors.blue
          ),
        child: new StatusPage())
      );
  }

这里是StatusPage,我在这里尝试使用DefaultTextStyle

代码语言:javascript
运行
复制
class StatusPage extends MyStatelessWidget {
  @override
  Widget build(BuildContext context) {
    TextStyle style = DefaultTextStyle.of(context).style;
    print("STYLE: $style");
    return new Scaffold(
      appBar: new AppBar(
        title: getText(context, 'app_title')
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text('wibble', style:style),
            new ActivityStatus(),
            new MonitoringStatus()]
          )));
  }
}

使用所示的代码,文本"wibble“将以适当的样式正确显示。我从文档中得到的理解是,默认情况下应该应用这种样式,因此我不需要为"wibble“的Text构造函数提供样式参数。

但是,如果删除样式参数,则无法从DefaultTextStyle中获取样式。

我遗漏了什么?

EN

回答 2

Stack Overflow用户

发布于 2018-04-13 18:42:04

像这样将DefaultTextStyle应用于Scaffold,您将在所有子文本小部件中获得此样式

代码语言:javascript
运行
复制
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new StatusPage());
  }
}

class StatusPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    TextStyle style = DefaultTextStyle.of(context).style;
    return new Scaffold(
        appBar: new AppBar(),
        body: new DefaultTextStyle(
            style: new TextStyle(
                inherit: true,
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                decoration: TextDecoration.underline,
                decorationColor: Colors.red,
                decorationStyle: TextDecorationStyle.wavy,
                color: Colors.blue),
            child: new Center(
              child: new Column(
                children: <Widget>[
                  new Text("hello"),
                ],
              ),
            )));
  }
}
票数 13
EN

Stack Overflow用户

发布于 2020-06-11 15:15:28

我以前也遇到过同样的问题,我认为当使用自定义字体或更改语言时(至少我是这样),我的解决方案是转到MaterialApp小部件,然后覆盖所有的textTheme属性,如下所示:

代码语言:javascript
运行
复制
fontFamily: "STCBold",
              textTheme: GoogleFonts.cairoTextTheme(textTheme).copyWith(
                  headline1: TextStyle(height: 1),
                  headline2: TextStyle(height: 1),
                  headline3: TextStyle(height: 1),
                  headline4: TextStyle(height: 1),
                  headline5: TextStyle(height: 1),
                  headline6: TextStyle(height: 1),
                  subtitle1: TextStyle(height: 1),
                  subtitle2: TextStyle(height: 1),
                  bodyText1: TextStyle(height: 1),
                  bodyText2: TextStyle(height: 1),
                  caption: TextStyle(height: 1),
                  button: TextStyle(height: 1),
                  overline: TextStyle(height: 1),
              ),

这将使所有的文本主题没有额外的填充,因此所有的文本将是紧凑的。但请确保在所有代码中使用style: Theme.of(context).textTheme.WHATEVERTEXT.

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49806705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档