美文网首页
Flutter实现Text渐变

Flutter实现Text渐变

作者: Jerck_NING | 来源:发表于2019-03-17 15:05 被阅读0次

    Flutter实现Text渐变

    • 首先展示效果图


      image.png
    • 自定义GradientText Widget(直接贴代码)
    import 'package:flutter/material.dart';
    
    class GradientText extends StatelessWidget {
     GradientText(this.data,
         {@required this.gradient,
           this.style,
           this.textAlign = TextAlign.left});
    
     final String data;
     final Gradient gradient;
     final TextStyle style;
     final TextAlign textAlign;
    
     @override
     Widget build(BuildContext context) {
       return ShaderMask(
         shaderCallback: (bounds) {
           return gradient.createShader(Offset.zero & bounds.size);
         },
         child: Text(
           data,
           textAlign: textAlign,
           style: (style == null)
               ? TextStyle(color: Colors.white)
               : style.copyWith(color: Colors.white),
         ),
       );
     }
    }
    
    • 使用
    ListTile getTitleItem(HeadingItem item, BuildContext context) {
        return new ListTile(
            title: new GestureDetector(
          child: new GradientText(
            item.heading,
            textAlign: TextAlign.left,
            gradient: LinearGradient(colors: [Colors.redAccent, Colors.green]),
            style: TextStyle(
              fontSize: 30.0,
            ),
          ),
          onTap: () {
            Navigator.push(
              context,
              new MaterialPageRoute(builder: (context) => new HomePage()),
            );
          },
        ));
      }
    

    相关文章

      网友评论

          本文标题:Flutter实现Text渐变

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