我是靠谱客的博主 烂漫蜜粉,这篇文章主要介绍Flutter的菜鸟教程八:侧滑删除,现在分享给大家,希望可以做个参考。

本文将介绍一个新的内容,侧滑删除,这在列表项中应用比较广泛,他就是Dismissible,想当然是一个widget,你可以想到它肯定不止可以用在列表项中,任何一个widget作为他的子项都可以实现滑动删除,这在Android中我们通常需要自定义控件来实现,但是在Flutter中.

gif太麻烦看下静态效果图,当然你可以脑补一下.

这里写图片描述

直接看代码,新内容都添加了注释

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; /** * 侧滑删除 */ void main() { runApp(new MaterialApp( title: "", home: new MyApp( items: new List<String>.generate(20, (i) => "Item ${i + 1}"), ))); } class MyApp extends StatelessWidget { final List<String> items; MyApp({Key key, @required this.items}) : super(key: key); @override Widget build(BuildContext context) { final title = "Dismissing Items"; return new MaterialApp( title: title, home: new Scaffold( appBar: new AppBar( title: new Text(title), ), body: new ListView.builder( itemCount: items.length, itemBuilder: (context, index) { final item = items[index]; //通过拖动来删除小部件的widget return new Dismissible( //如果Dismissible是一个列表项 它必须有一个key 用来区别其他项 key: new Key(item), //在child被取消时调用 onDismissed: (direction) { items.removeAt(index); //这个和Android的SnackBar差不多 Scaffold.of(context).showSnackBar( new SnackBar(content: new Text("$item dismissed"))); }, //如果指定了background 他将会堆叠在Dismissible child后面 并在child移除时暴露 background: new Container( color: Colors.red, ), child: new ListTile( title: new Text("$item"), )); }, ), ), ); } }

这个background的类型是一个widget,注释说的大概能明白了,看下官网的介绍

这里写图片描述

最后

以上就是烂漫蜜粉最近收集整理的关于Flutter的菜鸟教程八:侧滑删除的全部内容,更多相关Flutter内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部