了解Flutter的ShaderMask

时间:2019-08-12 01:43:09

标签: flutter flutter-layout

我试图了解ShaderMask的工作方式。如果我按照ShaderMask文档here上的示例进行操作:

ShaderMask(
  shaderCallback: (Rect bounds) {
    return RadialGradient(
      center: Alignment.topLeft,
      radius: 1.0,
      colors: <Color>[Colors.yellow, Colors.deepOrange.shade900],
      tileMode: TileMode.mirror,
    ).createShader(bounds);
  },
  child: const Text('I’m burning the memories'),
)

我明白了:

ShaderMask example output

Text下面的双线显然表明缺少Theme

然后,如果我将同一ShaderMask包围在Scaffold中,我会得到:

Scaffold(
      body: Center(
        child: ShaderMask(
          shaderCallback: (Rect bounds) {
            return RadialGradient(
              center: Alignment.topLeft,
              radius: 1.0,
              colors: <Color>[Colors.yellow, Colors.deepOrange.shade900],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          child: const Text('I’m burning the memories'),
        ),
      ),
    );

ShaderMask with Scaffold

一切都消失了! ShaderMask似乎已被忽略,并且文档中也没有提及此行为,为什么Scaffold““禁用”“ ShaderMask

1 个答案:

答案 0 :(得分:2)

您丢失了-blendMode属性

有关更多信息-blendMode property

工作代码:

    Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ShaderMask(
          blendMode: BlendMode.srcATop,  // Add this
          shaderCallback: (Rect bounds) {
            return RadialGradient(
              center: Alignment.topLeft,
              radius: 1.0,
              colors: <Color>[Colors.yellow, Colors.deepOrange.shade900],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          child: const Text('I’m burning the memories'),
        ),
      ),
    );
  }