flutter封装点击菜单工具栏组件checkBox多选版

 更新时间:2022年05月16日 16:30:53   作者:编程小龙  
这篇文章主要为大家介绍了flutter封装一个点击菜单工具栏组件,checkBox多选版的示例示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

效果展示

单选版可看上篇博文 用flutter封装一个点击菜单工具栏组件

本文是CHeckbox多选版

效果如图所示,点击选项回调选中的index和是否选中的值,可以自定义横向纵向,传递宽高后自动计算子项宽高,自定义边框、背景、选中的样式

实现代码

第一部分是封装子项组件, ToolMenuCheckboxItemWidget组件如下:

import 'dart:core';
import 'package:flutter/material.dart';
/// @author 编程小龙
/// @创建时间:2022/3/8
/// 工具菜单checkbox版子项
class ToolMenuCheckboxItemWidget extends StatelessWidget {
  /// 显示的title
  final String title;
  /// 序号
  final int index;
  /// 点击回调
  final void Function(int, bool) click;
  final double width;
  final double height;
  final bool isActive;
  final bool isHorizontal; // 是否横向
  final bool isEnd; // 是否为末尾
  final Color? activeColor; // 点击后的颜色 没传取主题色
  final Color? backgroundColor; // 背景色
  final Color? borderColor; // 边框色
  final TextStyle? textStyle; // 文字样式
  final TextStyle? activeTextStyle; //  选中的文字样式
  const ToolMenuCheckboxItemWidget(
      {Key? key,
      this.isActive = false,
      required this.title,
      required this.index,
      required this.click,
      this.isHorizontal = false,
      this.width = 100,
      this.isEnd = false,
      this.height = 40,
      this.activeColor,
      this.backgroundColor,
      this.borderColor,
      this.textStyle,
      this.activeTextStyle})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    TextStyle defaultTextStyle = TextStyle(
        fontSize: 16, color: isActive ? Colors.white : Colors.black87);
    return Material(
      child: Ink( // 点击右波纹效果
        width: width,
        height: height,
        decoration: BoxDecoration(
            color: isActive
                ? activeColor ?? Theme.of(context).primaryColor
                : backgroundColor ?? Colors.white30,
            border: isHorizontal
                ? isEnd
                ? const Border()
                : Border(
                right: BorderSide(
                    width: 1, color: borderColor ?? Colors.grey))
                : Border(
                bottom: BorderSide(
                    width: 1, color: borderColor ?? Colors.grey))),
        child: InkWell(
            onTap: () {
              click(index,!isActive);
            },
            child: Center(
              child: Text(title,
                  style: isActive
                      ? activeTextStyle ?? defaultTextStyle
                      : textStyle ?? defaultTextStyle),
            )),
      ),
    );
  }
}

第二部分是封装菜单内容,ToolMenuCheckBoxWidget代码如下

import 'package:demo/widgets/tool_menu_checkbox_item_widget.dart';
import 'package:flutter/material.dart';
/// @author 编程小龙
/// @创建时间:2022/3/8
/// 工具菜单checkbox版
class ToolMenuCheckBoxWidget extends StatefulWidget {
  final Map<String, bool> items; // title:checked 的形式 返回值为 key:true/false
  final void Function(int, bool) click; // 点击回调 返回第n个的选中情况
  final double? width;
  final double? height;
  final bool isHorizontal; // 横向
  final Color? activeColor; // 点击后的颜色 没传取主题色
  final Color? backgroundColor; // 背景色
  final Color? borderColor; // 边框色
  final TextStyle? textStyle; // 文字样式
  final TextStyle? activeTextStyle; //  选中的文字样式
  const ToolMenuCheckBoxWidget(
      {Key? key,
      required this.items,
      required this.click,
      this.width,
      this.height,
      this.isHorizontal = false,
      this.activeColor,
      this.backgroundColor,
      this.borderColor,
      this.textStyle,
      this.activeTextStyle})
      : super(key: key);
  @override
  State<ToolMenuCheckBoxWidget> createState() => _ToolMenuCheckBoxWidgetState();
}
class _ToolMenuCheckBoxWidgetState extends State<ToolMenuCheckBoxWidget> {
  late Map<String, bool> items;
  bool isHorizontal = false; // 是否横向
  @override
  void initState() {
    // 初始化当前选中
    items = widget.items;
    isHorizontal = widget.isHorizontal;
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    int index = 0; // 遍历自增 index
    int size = widget.items.length;
    double height = widget.height ?? (isHorizontal ? 50 : 200); // 设置水平和竖直时的默认值
    double width = widget.width ?? (isHorizontal ? 400 : 100);
    return Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: widget.backgroundColor ?? Colors.white30,
        border: Border.all(color: widget.borderColor ?? Colors.grey, width: 1),
      ),
      child: Wrap(
        children: items.keys.map((key) {
          return ToolMenuCheckboxItemWidget(
            title: key,
            index: index,
            isHorizontal: widget.isHorizontal,
            click: (index, isChecked) {
              setState(() {
                items[key] = isChecked;
              });
              widget.click(index, isChecked);
            },
            height:
                widget.isHorizontal ? height - 2 : height / size,
            isActive: items[key] ?? false,
            width: widget.isHorizontal ? width / size - 1 : width,
            isEnd: index++ == size - 1,
            textStyle: widget.textStyle,
            activeTextStyle: widget.activeTextStyle,
            backgroundColor: widget.backgroundColor,
            activeColor: widget.activeColor,
            borderColor: widget.borderColor,
          );
        }).toList(),
      ),
    );
  }
}

代码调用

最简单案例只需传入titles即可,选中颜色默认取主题颜色,后续再弄一个chekbox版的,可多选菜单

/// 竖向
ToolMenuCheckBoxWidget(
    items: { // 注意这里map不要用const声明,因为里面的值传递过去会同步更改,并不会重新copy一份值操作
      "选项1": true,
      "选项2": true,
      "选项3": false,
      "选项4": false
    },
    click: (index, isActive) {
      print("竖向 选项$index 的值为$isActive");
    }),
/// 自定义样式横向
ToolMenuCheckBoxWidget(
items: { // 注意这里map不要用const声明,因为里面的值传递过去会同步更改,并不会重新copy一份值操作
    "选项1": true,
    "选项2": false,
    "选项3": false,
    "选项4": true,
    "选项5": false,
    "选项6": false,
  },
  click: (index, isActive) {
    print("横向 选项$index 的值为$isActive");
  },
  isHorizontal: true,
  activeColor: Colors.green,
  backgroundColor: Colors.black,
  textStyle: const TextStyle(color: Colors.white),
  activeTextStyle:
  const TextStyle(color: Colors.white, fontSize: 18),
  borderColor: Colors.orange,
),

以上就是flutter封装一个点击菜单工具栏组件checkBox多选版的详细内容,更多关于flutter封装点击多选菜单组件的资料请关注脚本之家其它相关文章!

相关文章

  • Android最基本的异步网络请求框架

    Android最基本的异步网络请求框架

    这篇文章主要为大家详细介绍了Android最基本的异步网络请求框架,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • Android Picasso使用高斯模糊处理的示例代码

    Android Picasso使用高斯模糊处理的示例代码

    本篇文章主要介绍了Android Picasso使用高斯模糊处理的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android编程之判断SD卡状态的方法

    Android编程之判断SD卡状态的方法

    这篇文章主要介绍了Android编程之判断SD卡状态的方法,结合实例分析了Android针对SD卡的权限操作及状态判定技巧,需要的朋友可以参考下
    2016-02-02
  • Android对话框使用方法详解

    Android对话框使用方法详解

    这篇文章主要介绍了Android对话框使用方法,包括提示对话框、单选对话框、复选对话框、列表对话框等,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android实现中国象棋游戏(局域网版)

    Android实现中国象棋游戏(局域网版)

    这篇文章主要为大家详细介绍了Android实现局域网版的中国象棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 详解Android Webview加载网页时发送HTTP头信息

    详解Android Webview加载网页时发送HTTP头信息

    这篇文章主要介绍了详解Android Webview加载网页时发送HTTP头信息的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android系统中的蓝牙连接程序编写实例教程

    Android系统中的蓝牙连接程序编写实例教程

    这篇文章主要介绍了Android系统中的蓝牙连接程序编写实例教程,包括蓝牙的设备查找及自动配对等各种基础功能的实现,十分给力,需要的朋友可以参考下
    2016-04-04
  • android中知道图片name时获取图片的简单方法

    android中知道图片name时获取图片的简单方法

    android中知道图片name时获取图片的简单方法,需要的朋友可以参考一下
    2013-05-05
  • Kotlin select使用方法介绍

    Kotlin select使用方法介绍

    select是Kotlin 1.6中的特性,即选择最快的结果。select与async、Channel结合使用,可以大大提高程序的响应速度,还可以提高程序的灵活性、扩展性
    2022-11-11
  • Android自定义View实现绘制虚线的方法详解

    Android自定义View实现绘制虚线的方法详解

    这篇文章主要给大家介绍了Android自定义View实现绘制虚线的方法,在绘制过程中走了一些弯路才实现了虚线的效果,所以想着总结分享出来,方便有需要的朋友和自己在需要的时候参考学习,下面来一起看看吧。
    2017-04-04

最新评论