123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/components/alert_dialog.dart';
- class VDialogCheck<T, TValue> extends StatefulWidget {
- const VDialogCheck({
- Key? key,
- this.title,
- required this.source,
- required this.initialValue,
- this.labelGetter,
- required this.valueGetter,
- // ignore: non_constant_identifier_names
- this.MutexValue,
- }) : super(key: key);
- final String? title;
- final List<T> source;
- final List<TValue> initialValue;
- // ignore: non_constant_identifier_names
- final TValue? MutexValue;
- /// 选项文本提取函数
- final String Function(T data)? labelGetter;
- /// 选项值提取函数
- final TValue Function(T data) valueGetter;
- @override
- // ignore: library_private_types_in_public_api
- _VDialogCheckState<T, TValue> createState() =>
- _VDialogCheckState<T, TValue>();
- Future<T?> show<T>() => VAlertDialog.showDialog<T>(this);
- }
- class _VDialogCheckState<T, TValue> extends State<VDialogCheck<T, TValue>> {
- List<TValue> initialValue = [];
- @override
- void initState() {
- super.initState();
- initialValue = widget.initialValue.toList();
- }
- @override
- Widget build(BuildContext context) {
- return VAlertDialog(
- width: 460,
- title: widget.title,
- content: Scrollbar(
- thumbVisibility: true,
- child: SingleChildScrollView(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: _buildOptions(context),
- ),
- ),
- ),
- onConfirm: () {
- Get.back(result: initialValue);
- },
- );
- }
- List<Widget> _buildOptions(BuildContext context) {
- final primaryColor = Theme.of(context).primaryColor;
- final children = <Widget>[];
- for (var i = 0; i < widget.source.length; i++) {
- final data = widget.source[i];
- final val = widget.valueGetter(data);
- final isSelected = initialValue.contains(val);
- Widget label;
- label = Text(
- widget.labelGetter!(data),
- style: TextStyle(
- color: isSelected ? primaryColor : Colors.black,
- fontSize: 20,
- ),
- );
- final widgetItem = Container(
- alignment: Alignment.centerLeft,
- height: 56,
- padding: const EdgeInsets.symmetric(horizontal: 8),
- color: isSelected ? Theme.of(context).secondaryHeaderColor : null,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- const SizedBox(
- width: 20,
- ),
- label
- ],
- ),
- if (isSelected)
- Row(
- children: [
- Icon(
- Icons.check,
- size: 24,
- color: primaryColor,
- ),
- const SizedBox(
- width: 20,
- )
- ],
- )
- ],
- ),
- );
- children.add(
- Material(
- color: Colors.transparent,
- child: Ink(
- child: InkWell(
- onTap: () {
- final val = widget.valueGetter(data);
- // if (!(widget.MutexValue != null &&
- // initialValue.contains(widget.MutexValue)) ||
- // val == widget.MutexValue) {
- setState(() {
- if ((val == widget.MutexValue &&
- !initialValue.contains(widget.MutexValue))) {
- initialValue.clear();
- }
- if (val != widget.MutexValue &&
- initialValue.contains(widget.MutexValue)) {
- initialValue.remove(widget.MutexValue);
- }
- if (initialValue.contains(val)) {
- initialValue.remove(val);
- } else {
- initialValue.add(val);
- }
- });
- // }
- },
- child: widgetItem,
- )),
- ),
- );
- }
- return children;
- }
- }
|