简体   繁体   English

在 Flutter 中,是否可以增加 Dismissible 小部件的透明度?

[英]In Flutter is it possible to increase the transparency of a Dismissible widget the further it is dismissed?

I have a Dismissible widget in my application that I drag down to dismiss.我的应用程序中有一个 Dismissible 小部件,我可以向下拖动以关闭它。 There is a requirement that the transparency of the Dismissible should increase the further it is dragged down.有一个要求,Dismissible 的透明度应该随着它被拖得越深而增加。 So it should look as if it is fading out as it is dismissed.所以它应该看起来好像在它被解雇时逐渐消失。 If it were to be dragged back up, its transparency should decrease.如果将其向上拖动,则其透明度应降低。

As a simple test I tried wrapping the Dismissible in a Listener and Opacity widget.作为一个简单的测试,我尝试将 Dismissible 包装在 Listener 和 Opacity 小部件中。 The opacity value is set to a variable tracked in state.不透明度值设置为状态跟踪的变量。 The Listener widget listens to the total "y" axis movement of the Dismissible and when it reaches a certain threshold, decreases the the opacity value tracked in state. Listener 小部件侦听 Dismissible 的“y”轴总移动,当它达到某个阈值时,降低状态中跟踪的不透明度值。 See code below for example:例如,请参见下面的代码:

import 'package:flutter/material.dart';

class FadingDismissible extends StatefulWidget {
  @override
  _FadingDismissible createState() => _FadingDismissible();
}

class _FadingDismissible extends State<FadingDismissible> {
  double _totalMovement = 0;
  double _opacity;

  @override
  void initState() {
    super.initState();
    _opacity = 1.0;
  }

  _setOpacity(double opacityValue) {
    setState(() {
      _opacity = opacityValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerMove: (PointerMoveEvent event) {
        _totalMovement += event.delta.dy;
        if (_totalMovement > 200) {
          _setOpacity(0.5);
        }
      },
      onPointerUp: (PointerUpEvent event) {
        _setOpacity(1.0);
        _totalMovement = 0;
      },
      child: Opacity(
        opacity: _opacity,
        child: Dismissible(
          direction: DismissDirection.down,
          key: UniqueKey(),
          onDismissed: (direction) {
            Navigator.pop(context);
          },
          child: Scaffold(
            floatingActionButton: FloatingActionButton(
              onPressed: () {},
            ),
            body: Container(color: Colors.blue),
          ),
        ),
      ),
    );
  }
}

The issue is, whenever the state is set, the widget is re-built and the Dismissible jumps back to the top.问题是,无论何时设置状态,都会重新构建小部件,并且 Dismissible 会跳回到顶部。

Right now I'm not sure of another way around this.现在我不确定其他方法。 Is there a way to change the transparency of a Dismissible widget as it is dragged?有没有办法在拖动时更改 Dismissible 小部件的透明度? Or will I have to use a different widget altogether?还是我必须完全使用不同的小部件?

Thanks!谢谢!

I think might be the closest if you do not want to create your own Dismissible widget:如果您不想创建自己的 Dismissible 小部件,我认为可能是最接近的:

class FadingDismissible extends StatefulWidget {
  final String text;
  FadingDismissible({@required this.text});

  @override
  _FadingDismissibleState createState() => _FadingDismissibleState();
}

class _FadingDismissibleState extends State<FadingDismissible> {
  double opacity = 1.0;
  StreamController<double> controller = StreamController<double>();
  Stream stream;

  double startPosition;

  @override
  void initState() {
    super.initState();
    stream = controller.stream;
  }

  @override
  void dispose() {
    super.dispose();
    controller.close();
  }

  @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: GlobalKey(),
      child: StreamBuilder(
        stream: stream,
        initialData: 1.0,
        builder: (context, snapshot) {
          return Listener(
            child: Opacity(
              opacity: snapshot.data,
              child: Text(widget.text),
            ),
            onPointerDown: (event) {
              startPosition = event.position.dx;
            },
            onPointerUp: (event) {
              opacity = 1.0;
              controller.add(opacity);
            },
            onPointerMove: (details) {
              if (details.position.dx > startPosition) {
                var move = details.position.dx - startPosition;
                move = move / MediaQuery.of(context).size.width;

                opacity = 1 - move;

                controller.add(opacity);
              }
            },
          );
        },
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:已关闭的可关闭小部件仍然是树的一部分 - Flutter: a dismissed dismissible widget is still part of the tree 已关闭的 Dismissible 小部件仍然是 flutter 中树的一部分 - A dismissed Dismissible widget is still part of the tree in flutter 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” 在 flutter - How to fix 'A dismissed Dismissible widget is still part of the tree.' in flutter 已关闭的 Dismissible 小部件仍然是树的一部分。 在 flutter - A dismissed Dismissible widget is still part of the tree. In flutter 被关闭的 Dismissible 小部件仍然是树的一部分 - A dismissed Dismissible widget is still part of the tree Flutter Dismissed Dismissible 小部件仍然是树问题的一部分,无法解决 - Flutter Dismissed Dismissible widget still part of tree issue, can't resolve 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” flutter 错误 - How to fix "A dismissed Dismissible widget is still part of the tree." error in flutter 是否可以在flutter中为SliverList内的按钮实现一个可禁用的窗口小部件 - is it Possible to Implement a Dismissible widget for a button inside a SliverList in flutter 是否可以在 Flutter 中禁用可关闭? - Is it possible to disable a dismissible in Flutter? flutter可允许的小部件变量依赖性 - flutter Dismissible widget variable dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM