【Flutter组件】仿微信左滑删除组件与透明背景时的处理

本文用到的组件:git

这是一个简单版仿微信的左滑删除的组件:github

# 左滑删除组件
left_scroll_actions: any

pub地址:
https://pub.dartlang.org/pack...微信

仓库地址:
https://github.com/mjl0602/le...ide

问题

在flutter上,使用仿微信样式的左滑删除组件时,若是这一行的背景色是透明的,就会出现以下问题:ui

clipboard.png

透明的Row下能够看到删除和编辑按钮,咱们就须要处理一下。this

至于为啥要用透明背景色——由于Scaffold的背景色是渐变的……当时画设计图给本身挖的坑。spa

组件

分装很简单,把滑动进度和透明度挂钩,就能够了:设计

// 若是Row的背景必须是透明颜色的,就要作处理了:
class ExampleRow extends StatefulWidget {
  final Function onTap;
  final Function onEdit;
  final Function onDelete;

  const ExampleRow({
    Key key,
    this.onTap,
    this.onDelete,
    this.onEdit,
  }) : super(key: key);

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

class _ExampleRowState extends State<ExampleRow> {
  double opa = 0;
  @override
  Widget build(BuildContext context) {
    Widget myDeviceStatus = Icon(Icons.supervised_user_circle);
    Widget body = Container(
      padding: EdgeInsets.fromLTRB(12, 0, 0, 0),
      height: 92,
      width: double.infinity,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: Container(
              height: 66,
              width: 66,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(33),
              ),
              child: Placeholder(),
            ),
          ),
          Container(
            width: 44,
            height: double.infinity,
            child: Opacity(
              opacity: 1 - opa,
              child: Center(
                child: myDeviceStatus,
              ),
            ),
          )
        ],
      ),
    );

    List<Widget> actions = [
      Opacity(
        opacity: opa,
        child: MaterialButton(
          child: Container(
            color: Colors.white.withOpacity(0),
            alignment: Alignment.center,
            padding: EdgeInsets.all(20),
            child: Icon(Icons.delete, color: Colors.red),
          ),
          onTap: widget.onDelete,
        ),
      ),
      Opacity(
        opacity: opa,
        child: MaterialButton(
          child: Container(
            color: Colors.white.withOpacity(0),
            alignment: Alignment.center,
            padding: EdgeInsets.all(20),
            child: Icon(Icons.edit, color: Colors.blue),
          ),
          onTap: widget.onDelete,
        ),
      ),
    ];

    body = LeftScroll(
      child: body,
      buttonWidth: 70,
      buttons: actions,
      onTap: widget.onTap,
      onScroll: (a) {
        opa = a;
        setState(() {});
        print(a);
      },
      onSlideStarted: () {
        // opa = 0;
        setState(() {});
      },
      onSlideCompleted: () {
        opa = 1;
        setState(() {});
      },
      onSlideCanceled: () {
        opa = 0;
        setState(() {});
      },
    );

    return body;
  }
}

效果GIF

还能够,这个思路和UI效果都是能够接受的3d

gif

相关文章
相关标签/搜索