Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 颤振默认字体大小_Dart_Flutter_Widget - Fatal编程技术网

Dart 颤振默认字体大小

Dart 颤振默认字体大小,dart,flutter,widget,Dart,Flutter,Widget,我想在Flutter中为文本小部件设置一个默认字体大小。 我知道我可以在主题中设置默认字体系列,但没有默认的字体大小参数 我只是想知道我的自定义小部件是实现得很好,还是我用了错误的方法 import 'package:flutter/material.dart'; /// Custom Text with a default font Monospace and a default font size. class CustomText extends Text { /// Custom

我想在Flutter中为文本小部件设置一个默认字体大小。 我知道我可以在主题中设置默认字体系列,但没有默认的字体大小参数

我只是想知道我的自定义小部件是实现得很好,还是我用了错误的方法

import 'package:flutter/material.dart';

/// Custom Text with a default font Monospace and a default font size.
class CustomText extends Text {
  /// Custom Text Constructor extend of Text constructor.
  CustomText(this.dataCustom,
      {this.styleCustom = const TextStyle(), this.textAlignCustom})
      : super(dataCustom,
            style: styleCustom.copyWith(fontFamily: 'Monospace', fontSize: 12),
            textAlign: textAlignCustom);

  /// The text to display.
  ///
  /// This will be null if a [textSpan] is provided instead.
  final String dataCustom;

  /// If non-null, the style to use for this text.
  ///
  /// If the style's "inherit" property is true, the style will be merged with
  /// the closest enclosing [DefaultTextStyle]. Otherwise, the style will
  /// replace the closest enclosing [DefaultTextStyle].
  final TextStyle styleCustom;

  /// How the text should be aligned horizontally.
  final TextAlign textAlignCustom;
}
谢谢

你应该这么做


我找到了一种更好的默认字体大小的方法,即覆盖材质文本主题

参考:

例如: body1用于普通文本小部件 因此,对于所有文本小部件的红色

 theme: ThemeData(
        textTheme: TextTheme(body1: TextStyle(backgroundColor: Colors.red))
      )
结果:


fontSize:styleCustom.fontSize=无效的styleCustom.fontSize:10),
###您做得很对,除非您有默认值,如font size,但希望覆盖它##

在amorenew的答案上展开一点

您可以在MaterialApp()小部件中设置fontSize。但是请注意,它不会在所有小部件中工作,例如Flatbutton和ExpansionFile

void main() {
  runApp(myApp());
}

class myApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   return MaterialApp(
        title: "My Flutter App",
        theme: ThemeData(
          textTheme: TextTheme(body1: TextStyle(fontSize: 20.0)),
        ...
   );
  }
}
因此,如果希望样式也应用于平面按钮:

FlatButton(
   child:
      Text("my text",
         style: Theme.of(context).textTheme.body1,
      )
);
并且,如果希望fontSize与其他特定样式一起应用:

FlatButton(
   child:
      Text("my text",
         style:
            TextStyle(
               fontWeight: FontWeight.bold,
               color: Colors.black,
            fontSize: Theme.of(context).textTheme.body1.fontSize
            )
      )
);

颤振主题定义的不是一种默认字体大小,而是许多默认字体大小。使用的大小取决于具体情况,例如,文本小部件通常使用
正文
样式,但如果在按钮内部使用,则同一小部件将使用
按钮
样式

我找到了两种方法来增加颤振应用程序中的所有字体大小

简单解决方案:调整默认主题 生成的字体大小为(originalSize*fontSizeFactor+fontSizeDelta)。因此,在上面的示例中,所有字体大小增加10%,然后再增加2

具有更多控制的解决方案:手动定义所有尺寸
样式的完整列表可以在中找到。

您应该使用
DefaultTextStyle
小部件作为父小部件

应用于没有显式样式的子代文本小部件的文本样式

有关如何使用的示例:

返回DefaultTextStyle(
样式:TextStyle(字体大小:42,颜色:Colors.blue),
儿童:(……)
);

有几种可能性:

1-使用小部件

 DefaultTextStyle(
      style: TextStyle(
        fontSize: 17,
        fontWeight: FontWeight.bold,
       ),
      child: Text('Hello World') // I don't need to define a style for this Text widget anymore 
 ),
只需将此小部件用作父级
示例

 DefaultTextStyle(
      style: TextStyle(
        fontSize: 17,
        fontWeight: FontWeight.bold,
       ),
      child: Text('Hello World') // I don't need to define a style for this Text widget anymore 
 ),
输出:

 headline1, headline2, headline3, headline4, headline5, headline6, subtitle1, subtitle2, bodyText1, bodyText2, caption, button, overline, display4, display3, display2, display1, headline, title, subhead, subtitle, body2, body1
Text('Hello World' , style: Theme.of(context).textTheme.headline6,),

我不再需要为这个
文本
小部件定义样式,因为它
将默认为
DefaultTextStyle
小部件样式

另请参见:
,在给定的持续时间内平滑地设置文本样式更改的动画。
它使用提供的动画随时间平滑地设置文本样式更改的动画

2-使用预定义的

 DefaultTextStyle(
      style: TextStyle(
        fontSize: 17,
        fontWeight: FontWeight.bold,
       ),
      child: Text('Hello World') // I don't need to define a style for this Text widget anymore 
 ),
事实上,您所要做的就是选择一个预定义的textTheme并使用或修改它: 每个textTheme都有一个预定义的TextStyle,您可以直接使用或在使用前进行修改。
以下是预定义文本主题的列表:

 headline1, headline2, headline3, headline4, headline5, headline6, subtitle1, subtitle2, bodyText1, bodyText2, caption, button, overline, display4, display3, display2, display1, headline, title, subhead, subtitle, body2, body1
Text('Hello World' , style: Theme.of(context).textTheme.headline6,),
用法:

 headline1, headline2, headline3, headline4, headline5, headline6, subtitle1, subtitle2, bodyText1, bodyText2, caption, button, overline, display4, display3, display2, display1, headline, title, subhead, subtitle, body2, body1
Text('Hello World' , style: Theme.of(context).textTheme.headline6,),
输出:

您还可以更改此TextStyle的值,然后重新使用它

修改:

 headline1, headline2, headline3, headline4, headline5, headline6, subtitle1, subtitle2, bodyText1, bodyText2, caption, button, overline, display4, display3, display2, display1, headline, title, subhead, subtitle, body2, body1
Text('Hello World' , style: Theme.of(context).textTheme.headline6,),
把它放在你的小部件里

输出:

我的代码是

了解有关TextTheme的更多信息。

OP的问题迫切需要一个主题。这应该是公认的答案。非常感谢你给别人打分,但是你自己的答案比他的好。干得好。更喜欢约定而不是配置。这实际上是最好的答案。这正是我们所需要的。我不知道为什么它没有标记为Accepted这并不适用于任何地方,例如,当文本是FlatButton中的一个孩子时。另外,当问题是关于fontsize时,为什么示例显示如何设置默认背景色?您应该在内部使用fontSizeTextStyle@RobinManoli我的主要观点是使用主题,所以背景色可以作为一个例子