Flutter多项选择插件multi_select_item的使用
Flutter多项选择插件multi_select_item的使用
multi_select_item
是一个用于在 Flutter 的 GridView
和 ListView
中实现多选功能的插件。它允许用户通过点击或长按列表项来选择或取消选择项目。
使用步骤
1. 引入依赖
首先,在 pubspec.yaml
文件中添加 multi_select_item
依赖:
dependencies:
multi_select_item: ^1.0.0
然后运行 flutter pub get
来安装依赖。
2. 初始化控制器
在你的页面中定义一个 MultiSelectController
实例,并设置其长度以匹配你的数据源。
MultiSelectController controller = MultiSelectController();
// 初始化数据源
List mainList = [
{"key": "1"},
{"key": "2"},
{"key": "3"},
{"key": "4"}
];
[@override](/user/override)
void initState() {
super.initState();
controller.set(mainList.length); // 设置控制器长度
}
3. 使用 MultiSelectItem
在构建列表时,将 MultiSelectItem
包裹在每个列表项中。通过 isSelecting
参数控制是否处于多选模式,并通过 onSelected
处理用户的选择操作。
ListView.builder(
itemCount: mainList.length,
itemBuilder: (context, index) {
return MultiSelectItem(
isSelecting: controller.isSelecting, // 是否处于多选模式
onSelected: () {
setState(() {
controller.toggle(index); // 切换选中状态
});
},
child: Container(
child: ListTile(
title: Text("Title ${mainList[index]['key']}"), // 显示标题
subtitle: Text("Description ${mainList[index]['key']}"), // 显示描述
),
decoration: controller.isSelected(index) // 根据选中状态改变背景颜色
? BoxDecoration(color: Colors.grey[300])
: null,
),
);
},
),
4. 获取选中的索引
可以通过 controller.selectedIndexes
获取当前选中的索引列表。
print(controller.selectedIndexes.toString()); // 输出选中的索引
完整示例代码
以下是一个完整的示例代码,展示了如何使用 multi_select_item
插件来实现多选功能。
import 'package:flutter/material.dart';
import 'package:multi_select_item/multi_select_item.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Multi Select Item Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List mainList = [];
MultiSelectController controller = MultiSelectController();
[@override](/user/override)
void initState() {
super.initState();
mainList.addAll([
{"key": "1"},
{"key": "2"},
{"key": "3"},
{"key": "4"}
]);
controller.set(mainList.length);
}
void addItem() {
setState(() {
mainList.add({"key": mainList.length + 1});
controller.set(mainList.length);
});
}
void deleteItems() {
var selectedIndexes = controller.selectedIndexes;
selectedIndexes.sort((a, b) => b.compareTo(a)); // 按降序排序
selectedIndexes.forEach((index) {
mainList.removeAt(index);
});
setState(() {
controller.set(mainList.length);
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Selected ${controller.selectedIndexes.length}'),
actions: controller.isSelecting
? [
IconButton(
icon: Icon(Icons.select_all),
onPressed: () {
setState(() {
controller.toggleAll();
});
},
),
IconButton(
icon: Icon(Icons.delete),
onPressed: deleteItems,
),
]
: [],
),
body: ListView.builder(
itemCount: mainList.length,
itemBuilder: (context, index) {
return MultiSelectItem(
isSelecting: controller.isSelecting,
onSelected: () {
setState(() {
controller.toggle(index);
});
},
child: Container(
child: ListTile(
title: Text("Title ${mainList[index]['key']}"),
subtitle: Text("Description ${mainList[index]['key']}"),
),
decoration: controller.isSelected(index)
? BoxDecoration(color: Colors.grey[300])
: null,
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: addItem,
child: Icon(Icons.add),
),
);
}
}
1 回复