美文网首页Flutter系列教程
Flutter-RichText的使用,并在上面添加手势

Flutter-RichText的使用,并在上面添加手势

作者: 嗨哒哥 | 来源:发表于2020-01-23 22:27 被阅读0次

Flutter-RichText的使用,并在上面添加手势

1、RichText的定义

RichText在Flutter中类似于富文本的作用,可以利用RichText在一句话中展示各种不同样式的文字。
下面先看看RichText的常用定义:

RichText({
    Key key,
    @required this.text,//文本内容
    this.textAlign = TextAlign.start,//设置文字的起始位置,一共有left、right、start、end、center、justify几种情况
    this.textDirection,//设置文字流方向,当this.textAlign存在的时候,以this.textAlign为主
    this.overflow = TextOverflow.clip,//文字切割方式
    this.textScaleFactor = 1.0,//文字放大缩小倍数,默认为1.0
    this.maxLines,//最大展示行数
})

2、RichText的简单实用

RichText在这里以最简单的富文本的方式展示:

  Widget _richTextDemo(BuildContext context) {
    return Container(
        color: Colors.blueGrey,
        height: 100.0,
        width: MediaQuery.of(context).size.width,
        child: RichText(
            textAlign: TextAlign.right,
    //          textAlign: TextAlign.center,
            textDirection: TextDirection.ltr,
            text: TextSpan(
                text: 'RichText',
                style: TextStyle(
                    color: Colors.red,
                    fontSize: 30.0,
                    fontWeight: FontWeight.bold
                ),
                children: [
                  TextSpan(text: '.com',
                      style: TextStyle(
                          color: Colors.greenAccent,
                          fontSize: 20.0,
                          fontWeight: FontWeight.w300
                      )
                  )
                ]
            )
        )
    );
}

运行效果如下:


richtext1.png

3、RichText上添加点击手势和长按手势

富文本一个重要的作用就是可以点击,这里做一个简单的点击手势作为引子。
演示代码如下:

class RichDemo1 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _RichDemo1State();
}

class _RichDemo1State extends State<RichDemo1> {
  TapGestureRecognizer _tapGestureRecognizer;
  TapGestureRecognizer _tapGestureRecognizer1;
//  LongPressGestureRecognizer _longPressGestureRecognizer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tapGestureRecognizer = new TapGestureRecognizer();
    _tapGestureRecognizer1 = new TapGestureRecognizer()
    ..onTap = _handelLittleTapGes;
//    _longPressGestureRecognizer = new LongPressGestureRecognizer()
//    ..onLongPress = _handelLittleTapGes;
  }

  void dispose() {
    _tapGestureRecognizer.dispose();
    _tapGestureRecognizer1.dispose();
//    _longPressGestureRecognizer.dispose();
    super.dispose();
  }
  void _handelTapGes() {
    print('点击了大标题');
  }
  void _handelLittleTapGes() {
    print('点击了小标题');
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        color: Colors.blueGrey,
        height: 100.0,
        width: MediaQuery.of(context).size.width,
        child: RichText(
            textAlign: TextAlign.right,
    //          textAlign: TextAlign.center,
            textDirection: TextDirection.ltr,
            text: TextSpan(
                text: '可以点击的大标题',
                style: TextStyle(
                    color: Colors.red,
                    fontSize: 30.0,
                    fontWeight: FontWeight.bold
                ),
                recognizer: _tapGestureRecognizer..onTap = _handelTapGes,
                children: [
                  TextSpan(text: '点击小标题',
                      recognizer: _tapGestureRecognizer1,
                      style: TextStyle(
                          color: Colors.greenAccent,
                          fontSize: 20.0,
                          fontWeight: FontWeight.w300
                      )
                  )
                ]
            )
        )
    );
  }
}

运行代码,对大小标题进行点击,查看效果。

相关文章

  • Flutter-RichText的使用,并在上面添加手势

    Flutter-RichText的使用,并在上面添加手势 1、RichText的定义 RichText在Flutt...

  • iOS-手势

    手势使用方法 1.创建手势2.添加手势3.实现手势方法 添加点按手势 代理方法:是否允许接收手指 添加长按手势 添...

  • iOS UIView添加手势回调

    前言 · UIView是我们在开发中经常使用到的控件,很多时候需要在该控件上面添加手势处理。常规的做法就是实例手势...

  • iOS UIView添加手势回调

    前言 · UIView是我们在开发中经常使用到的控件,很多时候需要在该控件上面添加手势处理。常规的做法就是实例手势...

  • FDFullscreenPopGesture取消单页面侧滑操作

    1.通用取消侧滑手势,添加一个拖动手势覆盖侧滑 2.页面有滑动操作,如地图拖动手势,如果使用上面的方法会让整个页面...

  • swift(监听与响应)

    添加一个view并在view上加一个单击手势 响应单击手势 添加kvo监听 创建一个kvo监听的类,并定义一个要监...

  • iOS添加全屏返回手势

    全屏返回手势建议直接使用框架FDFullscreenPopGesture 1.先添加一个代理类 2.添加全屏手势方...

  • 使用storyboard添加手势

    添加手势:storyboard添加手势到图片.png 然后打开互动手势:

  • iOS 点击事件与手势冲突

    最近做的一个需求,view上面有一个按钮,按钮添加了一个事件,view上添加了一个手势,当点击按钮时会和手势冲突,...

  • Android录制视频并添加水印

    最近有需求,录制视频的时候要添加水印,怎么搞?最后决定,opengles作为相机预览并在上面绘制水印,使用medi...

网友评论

    本文标题:Flutter-RichText的使用,并在上面添加手势

    本文链接:https://www.haomeiwen.com/subject/skqazctx.html