美文网首页
Flutter 仿iOS左滑删除,长按拖动

Flutter 仿iOS左滑删除,长按拖动

作者: 灯红酒绿映不出的落寞 | 来源:发表于2022-07-28 10:06 被阅读0次
先上效果图
左滑删除.PNG Untitled4.gif
用到的plugin有1.左滑删除。 2.长按拖动
#拖动组件
  reorderables: ^0.4.0

  #左滑删除
  flutter_slidable: 1.3.0

创建左滑删除视觉
Widget _buildChildItem(PlaceChildItem childItem) {
    //滑动组件
    return Padding(
      key: ValueKey(childItem.id),
      padding: EdgeInsets.only(bottom: 15),
      child: Slidable(
        key: ValueKey(childItem.id),
        direction: Axis.horizontal,
        enabled: items.length <= 1 ? false : true,
        endActionPane:
            ActionPane(motion: ScrollMotion(), extentRatio: 0.5, children: [
//左右两个空的站位,为了视觉显示效果,
          SlidableAction(
            onPressed: (BuildContext context) {},
            label: "",
            backgroundColor: Colors.transparent,
          ),
          SlidableAction(
            flex: 5,
            onPressed: (BuildContext context) {
              _deleteItem(childItem);
            },
            icon: Icons.delete,
            label: "删除",
            borderRadius: BorderRadius.all(Radius.circular(12)),
            backgroundColor: Colors.orange,
          ),
          SlidableAction(
            onPressed: (BuildContext context) {},
            label: "",
            backgroundColor: Colors.transparent,
          ),
        ]),
//视觉上显示的widget,可根据需求自定义
        child: Container(
          height: 130,
          width: widget.width! - 30,
//        color: Colors.amber,
          padding: EdgeInsets.only(left: 20, right: 20, top: 10, bottom: 10),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(12)),
              image: DecorationImage(
                image: new ExactAssetImage(
                  'assets/images/weather_bg.jpg',
                ), //
                fit: BoxFit.fitWidth,
              )),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    childItem.name ?? "",
                    style: GpOtherTheme.size20Green(context)!.copyWith(
                        color: CommonColors.onPrimaryTextColor, fontSize: 27),
                  ),
                  Text(childItem.temperature ?? "",
                      style: GpOtherTheme.size20Green(context)!.copyWith(
                          color: CommonColors.onPrimaryTextColor,
                          fontSize: 40,
                          fontWeight: FontWeight.w300)),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    childItem.skycon ?? "",
                    style: GpOtherTheme.size14(context).copyWith(
                        color: CommonColors.onPrimaryTextColor, fontSize: 16),
                  ),
                  Text("${childItem.max ?? ""} ${childItem.min ?? ""}",
                      style: GpOtherTheme.size14(context).copyWith(
                          color: CommonColors.onPrimaryTextColor,
                          fontSize: 16)),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
创建可拖动视觉组件
Widget _buildReorderables() {
    _rows = items.map((e) => _buildChildItem(e)).toList();
    void _onReorder(int oldIndex, int newIndex) {
      if (mounted) {
        if (widget.onReorderCallback != null) {
          widget.onReorderCallback!(oldIndex, newIndex);
        }
        setState(() {
          Widget row = _rows.removeAt(oldIndex);
          _rows.insert(newIndex, row);
          PlaceChildItem childItem = items.removeAt(oldIndex);
          items.insert(newIndex, childItem);
        });
      }
    }

    return ReorderableColumn(
      padding: EdgeInsets.symmetric(vertical: 20, horizontal: 15),
      crossAxisAlignment: CrossAxisAlignment.start,
      children: _rows,
      onReorder: _onReorder,
      buildDraggableFeedback:
          (BuildContext context, BoxConstraints constraints, Widget child) {
//包裹的目的是,child有切圆角,防止长按拖动时,显示背景
        return Material(
          color: Colors.transparent,
          child: child,
        );
      },
    );
  }


代码比较少,通俗易懂,位置在lib --> sections --> weather --> widget --> place.dart line 208

相关文章

网友评论

      本文标题:Flutter 仿iOS左滑删除,长按拖动

      本文链接:https://www.haomeiwen.com/subject/rfotwrtx.html