当前位置:   article > 正文

flutter 生成单选组件

flutter 生成单选组件

一、效果图

在这里插入图片描述

二、主要代码

import 'package:company_manage_flutter/xcClass/dicDataProp.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CheckListWidget extends StatefulWidget {
  final List<Map<String, dynamic>> list;
  final Function? onChanged;
  final DicDataProp props;
  Map<String, dynamic>? initValue;
  CheckListWidget(
      {super.key,
      required this.list,
      this.onChanged,
      this.props = const DicDataProp(),
      this.initValue});

  @override
  State<CheckListWidget> createState() => CheckListWidgetState();
}

class CheckListWidgetState extends State<CheckListWidget> {
  RxMap<String, dynamic> selected = <String, dynamic>{}.obs;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    selected.value = widget.initValue ?? {};
  }

  @override
  Widget build(BuildContext context) {
    return buildCheckList(widget.list, widget.onChanged, props: widget.props);
  }

  void test() {
    print('test-组件内的方法');
  }

  //单选列表
  Widget buildCheckList(List<Map<String, dynamic>> list, Function? onChanged,
      {DicDataProp? props}) {
    String label = props?.label ?? 'label';
    String value = props?.value ?? 'value';
    return Obx(() => Container(
        width: Get.width,
        child: Column(
          children: list.asMap().entries.map((entry) {
            int index = entry.key;
            dynamic item = entry.value;
            return Column(
              children: [
                GestureDetector(
                    onTap: () {
                      selected?.value = item;
                      if (onChanged != null) {
                        onChanged(item);
                      }
                    },
                    child: Container(
                      width: Get.width,
                      decoration: BoxDecoration(
                        color: Colors.blue.withOpacity(0),
                      ),
                      padding: const EdgeInsets.symmetric(
                          vertical: 16, horizontal: 16),
                      child: Row(
                        children: [
                          Icon(
                              (selected?.value[value] ?? '') == item[value]
                                  ? Icons.check_circle
                                  : Icons.circle_outlined,
                              size: 22,
                              color:
                                  (selected?.value[value] ?? '') == item[value]
                                      ? Color.fromRGBO(50, 73, 223, 1)
                                      : Color.fromRGBO(21, 23, 30, 0.40)),
                          SizedBox(width: 6),
                          Text(
                            item[label] ?? "",
                            style: TextStyle(
                              fontSize: 16,
                            ),
                          ),
                        ],
                      ),
                    )),
                Divider(
                  height: 1,
                  color: index + 1 == list.length
                      ? Color.fromRGBO(128, 130, 145, 0)
                      : Color.fromRGBO(128, 130, 145, 0.20),
                ),
              ],
            );
          }).toList(),
        )));
  }
}

  • 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
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

三、使用

//CheckListWidgetState 不能已下划线开头,因为_开头是私有的,不能在其他文件中访问
  final checkListWidgetKey = GlobalKey<CheckListWidgetState>();
 CheckListWidget(
    key: checkListWidgetKey,
     list: bottomSheetList,
     onChanged: (value) {
       print("CheckListWidget:${value}");
     },
     initValue: const {'id': '18', 'name': '仓库1'},
     props: DicDataProp(label: 'name', value: 'id'),
   ),
   buildButtonWidget('调用组件的方法、获取值', onPressed: () {
     print(
         '调用组件的方法、获取值 = ${checkListWidgetKey.currentState?.selected}');
     checkListWidgetKey.currentState?.test();
   }),
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/518492
推荐阅读
相关标签
  

闽ICP备14008679号