Flutter多级联动下拉选择插件rashail_multi_dropdown的使用
Flutter多级联动下拉选择插件rashail_multi_dropdown的使用
预览
单选模式
RashailMultiDropdown(
onOptionSelected: (List<ValueItem> selectedOptions) {},
options: const [
ValueItem(label: '选项1', value: '1'),
ValueItem(label: '选项2', value: '2'),
ValueItem(label: '选项3', value: '3'),
ValueItem(label: '选项4', value: '4'),
ValueItem(label: '选项5', value: '5'),
ValueItem(label: '选项6', value: '6'),
],
selectionType: SelectionType.single,
chipConfig: const ChipConfig(wrapType: WrapType.wrap),
dropdownHeight: 300,
optionTextStyle: const TextStyle(fontSize: 16),
selectedOptionIcon: const Icon(Icons.check_circle),
),
多选模式
RashailMultiDropdown(
onOptionSelected: (List<ValueItem> selectedOptions) {},
options: const [
ValueItem(label: '选项1', value: '1'),
ValueItem(label: '选项2', value: '2'),
ValueItem(label: '选项3', value: '3'),
ValueItem(label: '选项4', value: '4'),
ValueItem(label: '选项5', value: '5'),
ValueItem(label: '选项6', value: '6'),
],
selectionType: SelectionType.multi,
chipConfig: const ChipConfig(wrapType: WrapType.wrap),
dropdownHeight: 300,
optionTextStyle: const TextStyle(fontSize: 16),
selectedOptionIcon: const Icon(Icons.check_circle),
),
网络请求
RashailMultiDropdown.network(
onOptionSelected: (options) {},
networkConfig: NetworkConfig(
url: 'https://jsonplaceholder.typicode.com/users',
method: RequestMethod.get,
headers: {
'Content-Type': 'application/json',
},
),
chipConfig: const ChipConfig(wrapType: WrapType.wrap),
responseParser: (response) {
final list = (response as List<dynamic>).map((e) {
final item = e as Map<String, dynamic>;
return ValueItem(
label: item['name'],
value: item['id'].toString(),
);
}).toList();
return Future.value(list);
},
responseErrorBuilder: ((context, body) {
return const Padding(
padding: EdgeInsets.all(16.0),
child: Text('错误获取数据'),
);
}),
)
功能
- 允许从列表中选择多个或单个项。
- 可以从URL获取数据。
- 显示所选项作为标签。可以自定义标签样式。
- 可以禁用下拉项。
- 可以预选下拉项。
- 可以自定义下拉项。
- 可以自定义所选项生成器。
- 可以自定义下拉字段样式。
- 在下拉项被选中或取消选中时触发回调。
开始使用
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
rashail_multi_dropdown: ^0.0.1
然后运行 flutter packages get
。
示例
import 'package:flutter/material.dart';
import 'package:rashail_multi_dropdown/rashail_multi_dropdown.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('WRAP', style: _headerStyle),
const SizedBox(height: 4),
RashailMultiDropdown(
onOptionSelected: (List<ValueItem> selectedOptions) {},
options: const [
ValueItem(label: '选项1', value: '1'),
ValueItem(label: '选项2', value: '2'),
ValueItem(label: '选项3', value: '3'),
ValueItem(label: '选项4', value: '4'),
ValueItem(label: '选项5', value: '5'),
ValueItem(label: '选项6', value: '6'),
],
selectionType: SelectionType.multi,
chipConfig: const ChipConfig(wrapType: WrapType.wrap),
dropdownHeight: 300,
optionTextStyle: const TextStyle(fontSize: 16),
selectedOptionIcon: const Icon(Icons.check_circle),
),
const SizedBox(height: 50),
const Text('SCROLL', style: _headerStyle),
const SizedBox(height: 4),
RashailMultiDropdown(
onOptionSelected: (List<ValueItem> selectedOptions) {},
options: const [
ValueItem(label: '选项1', value: '1'),
ValueItem(label: '选项2', value: '2'),
ValueItem(label: '选项3', value: '3'),
ValueItem(label: '选项4', value: '4'),
ValueItem(label: '选项5', value: '5'),
ValueItem(label: '选项6', value: '6'),
],
selectionType: SelectionType.multi,
chipConfig: const ChipConfig(wrapType: WrapType.scroll),
dropdownHeight: 300,
optionTextStyle: const TextStyle(fontSize: 16),
selectedOptionIcon: const Icon(Icons.check_circle),
),
const SizedBox(height: 50),
const Text('FROM NETWORK', style: _headerStyle),
const SizedBox(height: 4),
RashailMultiDropdown.network(
onOptionSelected: (options) {},
networkConfig: NetworkConfig(
url: 'https://jsonplaceholder.typicode.com/users',
method: RequestMethod.get,
headers: {
'Content-Type': 'application/json',
},
),
chipConfig: const ChipConfig(wrapType: WrapType.wrap),
responseParser: (response) {
final list = (response as List<dynamic>).map((e) {
final item = e as Map<String, dynamic>;
return ValueItem(
label: item['name'],
value: item['id'].toString(),
);
}).toList();
return Future.value(list);
},
responseErrorBuilder: ((context, body) {
return const Padding(
padding: EdgeInsets.all(16.0),
child: Text('错误获取数据'),
);
}),
),
],
),
),
),
);
}
}
static const _headerStyle = TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.red,
);
更多关于Flutter多级联动下拉选择插件rashail_multi_dropdown的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复