Flutter控件之Text Widgets

负责显示文本和定义显示样式的控件。

  • Text

    显示单一样式的文本

    new Text(
      'Hello, $_name! How are you?',
      textAlign: TextAlign.center,
      overflow: TextOverflow.ellipsis,
      style: new TextStyle(fontWeight: FontWeight.bold),
    )
    

    如果想让文本控件响应Touch事件,要把该控件放在GestureDetector控件里使用,如果是material design应用,可以考虑直接使用FlatButton实现可Touch的文本。

  • RichText

    显示丰富样式的文本

    new RichText(
      text: new TextSpan(
        text: 'Hello ',
        style: DefaultTextStyle.of(context).style,
        children: <TextSpan>[
          new TextSpan(text: 'bold', style: new TextStyle(fontWeight: FontWeight.bold)),
          new TextSpan(text: ' world!'),
        ],
      ),
    )
    
  • DefaultTextStyle

    默认文本样式

    Widget build(BuildContext context) {
        return new Container(
          color: Colors.white,
          child: new Directionality(
            textDirection: TextDirection.ltr,
            child: new DefaultTextStyle(
              style: new TextStyle(
                fontSize: 14.0,
                color: Colors.blue,
                decoration: TextDecoration.underline
              ),
              maxLines: 2,
              softWrap: true,
              overflow: TextOverflow.ellipsis,
              child: new Text('+++++++++++++++++++++++++++++++++++我是一段超长的文本++++++++++++++++++++++++++++++++++++++++++++')
            )
          )
        );
      }
    
  • Directionality

    主要控制文字方向

    Widget build(BuildContext context) {
        return new Container(
          color: Colors.white,
          child: new Directionality(
            textDirection: TextDirection.rtl,
            child: new Text('我是一段文本')
          )
        );
      }
    

Text控件本身有style、textDirection等属性,之所以独立出DefaultTextStyle控件专门管理样式、Directionality控件专门管理文字方向,是为了方便统一某个模块的样式,继承父控件属性,不需要每个元素都要去设置一遍。

DefaultTextStyle和Directionality是InheritedWidget的子类,InheritedWidget实现了一个发布/订阅的模式,当子控件调用inheritFromWidgetOfExactType方法获取父控件时,同时也把自己加入到InheritedWidget的订阅者列表里面,所以当InheritedWidget属性改变的时候,就会调起子组件didChangeDependencies方法去通知子组件。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈,柳暗花明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值