我在TextStyle中搜索阴影选项,但没有找到.所以我问:怎样才能在Flutter 中给文字添加阴影呢?有可能吗? 示例:

new Text(
"asd"
style: new TextStyle( 
//add shadow?
));

推荐答案

Flutter now provides a way to do this without any work-arounds, as documented in 100 and 101.

虽然这使得它可以进入更稳定的通道,但也可以使用BackdropFilter来伪造阴影.

screenshot

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,
          ),
        ),
      ),
    );
  }
}

或者,如果你不在乎模糊度,只需要把一些半透明的Text小部件堆叠在彼此不太精确的顶部,做成Stack.

这样地:

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,
          ),
        ],
      ),
    );
  }
}

Flutter相关问答推荐

为什么在Flutter中使用firebase数据库时获取引用时会划掉引用?

如何在flutter中获得类似属性的float(CSS属性)

Android Emulator冻结,但在调整窗口大小时解冻

如何在应用程序启动时解析和加载JSON文件?

为什么Build_Runner的输出为0?

设置Flutter 中的ListView.Builder()的最小高度

Flutter Android Google Sign-in error:ApiException:10在确认软件包名称、SHA-1 fingerprint 和设置同意页面后出现异常

dart /长笛中的返回早期模式和拆分方法

创建仅定义导出的文件是否有意义?

了解多Provider 和流

在 Flutter 中使用条件语句设置 ImageProvider Object 类型的背景图像

Flutter:在 pubspec.yaml 中找不到assets资源

在我使用 flutter_riverpod stateprovider 重新保存代码之前,UI 主题状态不会更新

如何解决需要一个标识符,但得到的是:.try 在:之前插入一个标识符.dart 错误

Flutter - 我需要显示文本,但应用程序不需要

如何从 Flutter Slider Widget 中移除发光效果?

Flutter 构建方法使用的是旧版本的变量?

如何从 showModalBottomSheet 中的 topRight 和 topLeft 移除平边

Flutter:当我通过 Transform.rotate 旋转水平 ListView 时,左右边缘被切断

带图像封面的 ElevatedButton