flutter 侧滑删除+侧滑显示删除按钮

1、侧滑删除

  1.1、Dismissible组件

2、侧滑显示删除按钮

  2.1、手势监听水平滑动

------------------------------------分割线--------------------------------------------------------

dismissRemove.dart   

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class Dismessremove extends StatelessWidget {
  final title = '滑动删除';
  final List<String> items  =  new List<String>.generate(20, (i) => "Item ${i + 1}");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context); // 返回
            }),
        title: new Text(title),
      ),
      body: new ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          final item = items[index];
          return new Dismissible(
            // Each Dismissible must contain a Key. Keys allow Flutter to
            // uniquely identify Widgets.
            key: new Key(item),
            // We also need to provide a function that will tell our app
            // what to do after an item has been swiped away.
            onDismissed: (direction) {
              items.removeAt(index);
              Scaffold.of(context).showSnackBar(
                  new SnackBar(content: new Text("$item dismissed")));
            },
            // Show a red background as the item is swiped away
            background: new Container(color: Colors.red),
            child: new ListTile(title: new Text('$item'),),
          );
        },
      ),
    );
  }
}

效果就是滑动一下就马上删除数据,非常的突兀。

dismissshow.dart  

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class Dismessshow extends StatefulWidget {
  @override
  _Dismessshow createState() => _Dismessshow();
}

class _Dismessshow extends State<Dismessshow> {
  final title = '滑动显示';
//  final List<String> items  =  [
//    '1','2'
//  ];
//  final List<Map> items  =  [
//    {'name': 'xxx', 'show': false}
//  ];
  final List<Map> items = new List<Map>.generate(20, (i) {
    return {'name': 'item${i}', 'show': false};
  });
  change(index) {
    print('xxx');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context); // 返回
            }),
        title: new Text(title),
      ),
      body: new ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          final item = items[index];
          return new GestureDetector(
//              onHorizontalDragStart:(startDetails){},
            onHorizontalDragEnd: (endDetails) {
              // 怎么判断方向还是个问题
              setState(() {
                items[index]['show'] =
                    items[index]['show'] == true ? false : true;  // items[index]['show']!= items[index]['show'] 这样不行为啥
              });
            },
            child: new Container(
              height: 40.0,
//              color: Colors.amber,
              padding: const EdgeInsets.only(left: 10.0),
              decoration: new BoxDecoration(
                  border: new Border(
                bottom: BorderSide(color: Colors.amber, width: 0.5),
              )),
              child: new Row(
                children: <Widget>[
                  item['show'] == true
                      ? new RaisedButton(
                          child: new Text('remove'),
                          onPressed: () {
                            print('click');
                            setState(() {
                              items.removeAt(index);
                            });
                            Scaffold.of(context).showSnackBar(
                                new SnackBar(content: new Text('${item["name"]} is dismissed')));
                          },
                          color: Colors.yellow,
                          splashColor: Colors.pink[100])
                      : new Text(''),
                  new Text(item['name'])
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

效果如下

          

 github:https://github.com/ft1107949255/kiminitodoke

转载于:https://www.cnblogs.com/shuangzikun/p/taotao_flutter_remove.html

Flutter 是一种跨平台的移动应用开发框架,它提供了丰富的组件和功能,可以帮助开发者快速构建漂亮、流畅的用户界面。对于可滑动自动滚动折线图,在 Flutter 中可以使用 `ListView` 和 `AnimatedContainer` 组件来实现。 首先,我们可以使用 `ListView` 组件来创建一个可以滑动的容器。使用 `ListView.builder` 构建一个动态列表,将折线图中的数据作为列表项进行展示。在 `ListView` 内部添加一个 `ScrollController`,用来控制列表的滚动。 当需要自动滚动时,我们可以通过动画来实现。使用 `AnimatedContainer` 组件来包裹折线图,通过修改它的宽度来实现滚动效果。可以在需要的时候,通过调用 `setState` 方法,来更新 `AnimatedContainer` 的属性值,从而触发动画效果。 在滚动时,可以监听滚动的位置,根据当前滚动的位置来判断是否需要自动滚动。通过 `ScrollController` 的 `addListener` 方法监听滚动事件,计算滚动的位置,并进行相应的判断,如果需要自动滚动,就通过修改 `AnimatedContainer` 的属性值来触发动画。 同时,可以为 `AnimatedContainer` 设置合适的动画时长和曲线,来使滚动效果更加顺滑。 总结来说,要实现可滑动自动滚动折线图,可以使用 `ListView` 和 `AnimatedContainer` 组件。通过监听滚动事件,根据滚动的位置进行判断,并通过修改 `AnimatedContainer` 的属性值来触发动画效果,从而实现自动滚动的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值