flutter怎么封装点击菜单工具栏组件checkBox多选版
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这篇"flutter怎么封装点击菜单工具栏组件checkBox多选版"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所
千家信息网最后更新 2025年02月23日flutter怎么封装点击菜单工具栏组件checkBox多选版
这篇"flutter怎么封装点击菜单工具栏组件checkBox多选版"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"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 Mapitems; // 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 createState() => _ToolMenuCheckBoxWidgetState();}class _ToolMenuCheckBoxWidgetState extends State { late Map 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多选版"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
菜单
工具
组件
封装
内容
样式
横向
工具栏
文字
颜色
主题
代码
子项
效果
背景
边框
小龙
文章
时间
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
南京二手服务器回收推荐
集美网络技术培训机构
街道网络安全周宣传活动总结
大学数据库一对一辅导
北京管理软件开发如何收费
腾讯云服务器玩游戏画面卡顿
网络技术主管的职责是什么
服务器假面板 图
网络安全文献摘抄
后端网络技术支撑服务人员学习
县委网络安全化和信息化委
天龙八部刀锋破晓服务器怎么样
大专学计算机网络技术可以吗
东莞市一橙网络技术
昆明魅朗网络技术
网络安全防护系统的目标
远程服务器怎么连接矿池
阴阳师官网连接到服务器失败
大话西游沧州府什么时候开服务器
黄埔app软件开发定制
查看数据库最大连接数
广州中智软件开发是外包吗
国家网络安全委员会成立
群晖nas 传文件到服务器
亚马逊网络技术哪里去学
联想ts80x服务器出厂编号在哪里看
数据库控制中适用于触发器
数据库的表为什么保存不了
汽车网络技术试卷答案
数据库表查询第一列