How to add shadow to the text in flutter?

后端 未结 4 1928
鱼传尺愫
鱼传尺愫 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:20

    Here is a little playing around with the opacity, offset, and shadow radius:

    The full code is here. Try it out yourself.

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: ShadowDemo(),
          ),
        );
      }
    }
    
    class ShadowDemo extends StatefulWidget {
      @override
      _ShadowDemoState createState() => _ShadowDemoState();
    }
    
    class _ShadowDemoState extends State {
      var _opacity = 1.0;
      var _xOffset = 0.0;
      var _yOffset = 0.0;
      var _blurRadius = 0.0;
    
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: [
            Center(
                child: Text(
              'Flutter',
              style: TextStyle(
                fontSize: 100,
                color: Colors.blue.shade700,
                shadows: [
                  Shadow(
                    color: Colors.blue.shade900.withOpacity(_opacity),
                    offset: Offset(_xOffset, _yOffset),
                    blurRadius: _blurRadius,
                  ),
                ],
              ),
            )),
            Align(
              alignment: Alignment.bottomCenter,
              child: Padding(
                padding: const EdgeInsets.only(bottom: 80.0),
                child: Column(
                  children: [
                    Spacer(),
                    Slider(
                      value: _opacity,
                      min: 0.0,
                      max: 1.0,
                      onChanged: (newValue) =>
                          {setState(() => _opacity = newValue)},
                    ),
                    Slider(
                      value: _xOffset,
                      min: -100,
                      max: 100,
                      onChanged: (newValue) =>
                          {setState(() => _xOffset = newValue)},
                    ),
                    Slider(
                      value: _yOffset,
                      min: -100,
                      max: 100,
                      onChanged: (newValue) =>
                          {setState(() => _yOffset = newValue)},
                    ),
                    Slider(
                      value: _blurRadius,
                      min: 0,
                      max: 100,
                      onChanged: (newValue) =>
                          {setState(() => _blurRadius = newValue)},
                    ),
                  ],
                ),
              ),
            )
          ],
        );
      }
    }
    

提交回复
热议问题