How to add shadow to the text in flutter?

后端 未结 4 1927
鱼传尺愫
鱼传尺愫 2021-01-30 21:47

I searched for the shadow option in TextStyle, but I didn\'t find it. So I ask: how can I add shadow to the text in flutter? Is it possible? Example:

new Text(
\         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 22:26

    Flutter now provides a way to do this without any work-arounds, as documented in issue 3402 and Gary Qian's answer below.

    While this makes its way into the more stable channels, it's possible to fake a shadow using BackdropFilter.

    import 'dart:ui' as ui;
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MaterialApp(
        home: new MyApp(),
      ));
    }
    
    class ShadowText extends StatelessWidget {
      ShadowText(this.data, { this.style }) : assert(data != null);
    
      final String data;
      final TextStyle style;
    
      Widget build(BuildContext context) {
        return new ClipRect(
          child: new Stack(
            children: [
              new Positioned(
                top: 2.0,
                left: 2.0,
                child: new Text(
                  data,
                  style: style.copyWith(color: Colors.black.withOpacity(0.5)),
                ),
              ),
              new BackdropFilter(
                filter: new ui.ImageFilter.blur(sigmaX: 2.0, sigmaY: 2.0),
                child: new Text(data, style: style),
              ),
            ],
          ),
        );
      }
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Container(
            child: new Center(
              child: new ShadowText(
                'Hello world!',
                style: Theme.of(context).textTheme.display3,
              ),
            ),
          ),
        );
      }
    }
    

    Or if you don't care about the blur, just make a Stack with a few some semitransparent Text widgets stacked not quite precisely on top of each other.

    Like this:

    import 'package:flutter/material.dart';
    
    class ShadowText extends StatelessWidget {
    
      final String data;
      final TextStyle style;
      final TextAlign textAlign;
      final TextDirection textDirection;
      final bool softWrap;
      final TextOverflow overflow;
      final double textScaleFactor;
      final int maxLines;
    
      const ShadowText(this.data, {
        Key key,
        this.style,
        this.textAlign,
        this.textDirection,
        this.softWrap,
        this.overflow,
        this.textScaleFactor,
        this.maxLines,
      }) : assert(data != null);
    
      Widget build(BuildContext context) {
        return new ClipRect(
          child: new Stack(
            children: [
              new Positioned(
                top: 2.0,
                left: 2.0,
                child: new Text(
                  data,
                  style: style.copyWith(color: Colors.black.withOpacity(0.5)),
                  textAlign: textAlign,
                  textDirection: textDirection,
                  softWrap: softWrap,
                  overflow: overflow,
                  textScaleFactor: textScaleFactor,
                  maxLines: maxLines,
                ),
              ),
              new Text(
                data,
                style: style,
                textAlign: textAlign,
                textDirection: textDirection,
                softWrap: softWrap,
                overflow: overflow,
                textScaleFactor: textScaleFactor,
                maxLines: maxLines,
              ),
            ],
          ),
        );
      }
    }
    

提交回复
热议问题