flutter defaultTextStyle

source article: https://www.woolha.com/tutorials/flutter-using-defaulttextstyle-examples

 

在做一个功能的时间想到,如果一个 Text 没有设置style,它的默认style来自哪里?

 

This tutorial shows you how to use DefaultTextStyle widget in Flutter.

For each Text widget, Flutter allows you to define a different TextStyle. What if you want to apply the same style for multiple widgets. It can be done easily using DefaultTextStyle.

DefaultTextStyle is used to apply a default text style to its descendant widgets. Therefore, the DefaultTextStyle widget must be the ascendant of the widgets where the styles would be applied. But it doesn't mean a Text widget must follow the default style. It's still possible for a widget to have its own style.

 

Code Examples

Let's take a look at the below example.

import 'package:flutter/material.dart';
  import 'package:flutter/rendering.dart';
  
  void main() => runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Welcome to Flutter',
        home: MyAppHome(),
        theme: ThemeData(textTheme: TextTheme(body1: TextStyle(fontSize: 20.0)),),
      );
    }
  }
  
  class MyAppHome extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Flutter DefaultTextStyle Tutorial by Woolha.com'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            DefaultTextStyle(
              style: TextStyle(fontSize: 36, color: Colors.blue),
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Text(
                      'The first text',
                    ),
                    const Text(
                      'The second text',
                      style: TextStyle(fontSize: 24),
                    ),
                    const Text(
                      'The third text',
                      style: TextStyle(color: Colors.red),
                    ),
                  ],
                ),
              ),
            ),
            const Text(
              'The fourth text',
            ),
          ],
        )
      );
    }
  }
First, we define a DefaultTextStyle with a font size of 36 and blue as the font color. There are three Text widgets under it. The first one doesn't have its own style, so it uses the default style. The second one has its own style, but it only overrides the font size. Therefore, it still uses blue as the color, but with different font size. The third font overrides the color only, so it still uses the default size.

 

There is also a fourth Text widget, but it's outside DefaultTextStyle, so it's not affected by the default style.

Output:

flutter-defaulttextstyle-1.jpg

What if there is another DefaultTextStyle widget under the outer one. To know what will happen, change the body of the above code into this.

body: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      DefaultTextStyle(
        style: TextStyle(fontSize: 36, color: Colors.blue),
        child: DefaultTextStyle(
          style: TextStyle(color: Colors.green),
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'The first text',
                ),
                const Text({1}
                  'The second text',
                  style: TextStyle(fontSize: 24),
                ),
                const Text(
                  'The third text',
                  style: TextStyle(color: Colors.red),
                ),
              ],
            ),
          ),
        ),
      ),
      const Text(
        'The fourth text',
      ),
    ],
  ),

You'll find that the widgets use the nearest (the most specific) one. But the inner DefaultTextStyle stands as a completely new style, not inheriting the values from its ascendant widget of the same type.

Output:

flutter-defaulttextstyle-2.jpg

 

DefaultTextStyle Properties

Not only TextSyle that you can define, there are also some other properties that can be set as the default for its descendants. Below are the available properties you can pass in the constructor.

  • Key key: The widget key, used to control if it's should be replaced.
  • TextsStyle style*: Default TextStyle for its descendants.
  • TextAlign textAlign: How the text should be aligned.
  • boolean softWrap: Whether the text should break at soft line breaks.
  • Overflow overflow: How visual overflow should be handled.
  • int maxLines: maximum number of lines for the text.
  • TextWidthBasis textWidthBasis: The strategy to use for calculating the width of the Text.

 

*: required

For font family, you can also set it as the theme's default. Read our tutorial about font family which includes how to use custom font families on your application.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值