【问题标题】:How to create this linear fading opacity effect in flutter for android?如何在 Flutter for android 中创建这种线性渐变不透明度效果?
【发布时间】:2020-10-28 02:50:49
【问题描述】:

您好,我是 Flutter 的新手,我想创建这个设计,但我对如何为图像中标记的文本创建这种线性不透明阴影效果叠加层感到困惑

我圈出来的这个图标叫什么名字?

【问题讨论】:

标签: android flutter dart flutter-layout


【解决方案1】:

您可以使用带有 LinearGradient 的 CustomPainter:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: CustomPaint(
        foregroundPainter: FadingEffect(),
        //child gets the fading effect
        child: Text(
            'Test text',
            style: TextStyle(color: Colors.black)),
      ),
    );
  }
}

class FadingEffect extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Rect rect = Rect.fromPoints(Offset(0, 0), Offset(size.width, size.height));
    LinearGradient lg = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          //create 2 white colors, one transparent
          Color.fromARGB(0, 255, 255, 255),
          Color.fromARGB(255, 255, 255, 255)
        ]);
    Paint paint = Paint()..shader = lg.createShader(rect);
    canvas.drawRect(rect, paint);
  }

  @override
  bool shouldRepaint(FadingEffect linePainter) => false;
}

【讨论】:

  • 非常感谢你的回答......你真棒......
【解决方案2】:

您可以使用 ShaderMask 来实现这一点,下面我提供了一个可以模糊垂直边缘或水平边缘的示例。

class FadedEdges extends StatelessWidget {
  const FadedEdges(
      {Key? key,
      required this.child,
      this.colors,
      this.stops,
      this.isHorizontal = false})
      : super(key: key);
  final Widget child;
  final List<Color>? colors;
  final List<double>? stops;
  final bool isHorizontal;

  @override
  Widget build(BuildContext context) {
    return ShaderMask(
            blendMode: BlendMode.dstOut,
            shaderCallback: (Rect rect) => LinearGradient(
                    colors: colors ??
                        [
                          Colors.white.withOpacity(0.80),
                          Colors.transparent,
                          Colors.transparent,
                          Colors.white.withOpacity(0.80)
                        ],
                    stops: stops ?? const [0.1, 0.15, 0.85, 1.0],
                    begin: !isHorizontal
                        ? Alignment.topCenter
                        : Alignment.centerLeft,
                    end: !isHorizontal
                        ? Alignment.bottomCenter
                        : Alignment.centerRight)
                .createShader(rect),
            child: child)
        ;
  }
}

【讨论】:

    【解决方案3】:

    容器也有渐变...所以易于使用请检查此代码...并将颜色设置为colors.Transparent

        Container(
              width: double.infinity,
              height: 200,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [Colors.green,Colors.red],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  )
              ),
            )
    

    【讨论】:

      猜你喜欢
      • 2011-05-02
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 2021-06-15
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多