|
@@ -1,60 +1,49 @@
|
|
|
import 'package:flutter/material.dart';
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
+import 'alert_dialog.dart';
|
|
|
+
|
|
|
class VDialogSelect<T, TValue> extends StatelessWidget {
|
|
|
+ /// 数据集
|
|
|
final List<T> source;
|
|
|
- final TValue? value;
|
|
|
+
|
|
|
+ /// 初始值
|
|
|
+ final TValue? initialValue;
|
|
|
+
|
|
|
+ /// 弹窗标题
|
|
|
final String? title;
|
|
|
+
|
|
|
+ /// 选项文本提取函数
|
|
|
final String Function(T data)? labelGetter;
|
|
|
+
|
|
|
+ /// 选项外显组件构建器
|
|
|
final Widget Function(T data)? labelBuilder;
|
|
|
+
|
|
|
+ /// 选项值提取函数
|
|
|
final TValue Function(T data) valueGetter;
|
|
|
- final ValueChanged<TValue>? onChanged;
|
|
|
|
|
|
const VDialogSelect({
|
|
|
super.key,
|
|
|
required this.source,
|
|
|
this.title,
|
|
|
- this.value,
|
|
|
+ this.initialValue,
|
|
|
this.labelGetter,
|
|
|
this.labelBuilder,
|
|
|
required this.valueGetter,
|
|
|
- this.onChanged,
|
|
|
}) : assert(labelGetter != null || labelBuilder != null);
|
|
|
|
|
|
- void show() {
|
|
|
- Get.dialog(this);
|
|
|
- }
|
|
|
+ Future<T?> show<T>() => VAlertDialog.showDialog<T>(this);
|
|
|
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
- return AlertDialog(
|
|
|
- backgroundColor: Colors.white,
|
|
|
- elevation: 0,
|
|
|
- title: title != null ? Text(title!) : null,
|
|
|
- titleTextStyle: const TextStyle(fontSize: 24, color: Colors.black),
|
|
|
- actionsAlignment: MainAxisAlignment.center,
|
|
|
- actions: [
|
|
|
- SizedBox(
|
|
|
- width: 90,
|
|
|
- child: TextButton(
|
|
|
- onPressed: () {
|
|
|
- Navigator.of(context).pop();
|
|
|
- },
|
|
|
- child: const Text(
|
|
|
- "取消",
|
|
|
- style: TextStyle(fontSize: 18),
|
|
|
- ),
|
|
|
- ),
|
|
|
- ),
|
|
|
- ],
|
|
|
- contentPadding: const EdgeInsets.only(top: 20, bottom: 8),
|
|
|
- content: SizedBox(
|
|
|
- width: 460,
|
|
|
- child: Column(
|
|
|
- mainAxisSize: MainAxisSize.min,
|
|
|
- children: _buildOptions(context),
|
|
|
- ),
|
|
|
+ return VAlertDialog(
|
|
|
+ width: 460,
|
|
|
+ title: title,
|
|
|
+ content: Column(
|
|
|
+ mainAxisSize: MainAxisSize.min,
|
|
|
+ children: _buildOptions(context),
|
|
|
),
|
|
|
+ showCancel: true,
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -64,7 +53,7 @@ class VDialogSelect<T, TValue> extends StatelessWidget {
|
|
|
for (var i = 0; i < source.length; i++) {
|
|
|
final data = source[i];
|
|
|
final val = valueGetter(data);
|
|
|
- final isSelected = value == val;
|
|
|
+ final isSelected = initialValue == val;
|
|
|
Widget label;
|
|
|
if (labelBuilder != null) {
|
|
|
label = labelBuilder!(data);
|
|
@@ -108,7 +97,9 @@ class VDialogSelect<T, TValue> extends StatelessWidget {
|
|
|
color: Colors.transparent,
|
|
|
child: Ink(
|
|
|
child: InkWell(
|
|
|
- onTap: () {},
|
|
|
+ onTap: () {
|
|
|
+ _onSelected(val);
|
|
|
+ },
|
|
|
child: widget,
|
|
|
),
|
|
|
),
|
|
@@ -117,4 +108,8 @@ class VDialogSelect<T, TValue> extends StatelessWidget {
|
|
|
}
|
|
|
return children;
|
|
|
}
|
|
|
+
|
|
|
+ void _onSelected(TValue value) {
|
|
|
+ Get.back(result: value);
|
|
|
+ }
|
|
|
}
|