iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >flutter怎么封装单选点击菜单工具栏组件
  • 550
分享到

flutter怎么封装单选点击菜单工具栏组件

2023-06-30 13:06:41 550人浏览 安东尼
摘要

这篇“Flutter怎么封装单选点击菜单工具栏组件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“flutter怎么封装单选点

这篇“Flutter怎么封装单选点击菜单工具栏组件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“flutter怎么封装单选点击菜单工具栏组件”文章吧。

效果展示

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

本文是单选版

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

flutter怎么封装单选点击菜单工具栏组件

实现代码

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

import 'dart:core';import 'package:flutter/material.dart';/// @author 编程小龙/// @创建时间:2022/3/8/// 工具菜单子项class ToolMenuItemWidget extends StatelessWidget {  /// 显示的title  final String title;  /// 当前选中  final int index;  /// 点击回调  final ValueChanged<int> 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 ToolMenuItemWidget({    Key? key,    this.isActive = false,    required this.title,    required this.index,    required this.click,    this.activeColor,    this.backgroundColor,    this.borderColor,    this.textStyle,    this.activeTextStyle,    this.isHorizontal = false,    this.width = 100,    this.isEnd = false,    this.height = 40,  }) : super(key: key);  @override  Widget build(BuildContext context) {    var 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);            },            child: Center(              child: Text(title,                  style: isActive                      ? activeTextStyle ?? defaultTextStyle                      : textStyle ?? defaultTextStyle),            )),      ),    );  }}

第二部分是封装工具栏部分, ToolMenuItemWidget组件如下:

import 'package:demo/widgets/tool_menu_item_widget.dart';import 'package:flutter/material.dart';/// @author 编程小龙/// @创建时间:2022/3/8/// 工具菜单class ToolMenuWidget extends StatefulWidget {  final List<String> titles;  final ValueChanged<int> click; // 点击回调  final double? width;  final double? height;  final int currentIndex; // 当前选中  final bool isHorizontal; // 横向  final Color? activeColor; // 点击后的颜色 没传取主题色  final Color? backgroundColor; // 背景色  final Color? borderColor; // 边框色  final TextStyle? textStyle; // 文字样式  final TextStyle? activeTextStyle; //  选中的文字样式  const ToolMenuWidget(      {Key? key,      this.currentIndex = 0,      required this.titles,      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<ToolMenuWidget> createState() => _ToolMenuWidgetState();}class _ToolMenuWidgetState extends State<ToolMenuWidget> {  int currentIndex = 0; // 当前选中  bool isHorizontal = false; // 是否横向  @override  void initState() {    // 初始化当前选中    currentIndex = widget.currentIndex;    isHorizontal = widget.isHorizontal;    super.initState();  }  @override  Widget build(BuildContext context) {    int index = 0; // 用于遍历计数    int size = widget.titles.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: widget.titles.map((title) {          return ToolMenuItemWidget(            title: title,            index: index,            isHorizontal: widget.isHorizontal,            click: (index) {              setState(() {                currentIndex = index;              });              widget.click(index);            },            activeColor: widget.activeColor,            backgroundColor: widget.backgroundColor,            borderColor: widget.borderColor,            textStyle: widget.textStyle,            height: widget.isHorizontal ? height - 2 : height / size,            // 竖直状态-2 是去掉边框所占像素            isActive: index == currentIndex,            width: widget.isHorizontal ? width / size - 1 : width,            isEnd: index++ == size - 1,          );        }).toList(),      ),    );  }}

代码调用

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

/// 竖向,默认样式ToolMenuWidget(   titles: const ["选项1", "选项2", "选项3", "选项4"],   click: (index) {     print(" 竖向选中的是 $index");   }, ),/// 自定义样式横向ToolMenuWidget(  titles: const ["选项1", "选项2", "选项3", "选项4","选项5"],   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,   click: (index) {     print("横向选中的是 $index");   }, )

以上就是关于“flutter怎么封装单选点击菜单工具栏组件”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。

--结束END--

本文标题: flutter怎么封装单选点击菜单工具栏组件

本文链接: https://www.lsjlt.com/news/329489.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • flutter封装单选点击菜单工具栏组件
    目录效果展示实现代码代码调用效果展示 CHeckbox多选版 flutter封装点击菜单工具栏组件 本文是单选版 效果如图所示,点击选项回调选中的index,可以自定义横向...
    99+
    2024-04-02
  • flutter怎么封装单选点击菜单工具栏组件
    这篇“flutter怎么封装单选点击菜单工具栏组件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“flutter怎么封装单选点...
    99+
    2023-06-30
  • flutter封装点击菜单工具栏组件checkBox多选版
    目录效果展示实现代码代码调用效果展示 单选版可看上篇博文 用flutter封装一个点击菜单工具栏组件 本文是CHeckbox多选版 效果如图所示,点击选项回调选中的inde...
    99+
    2024-04-02
  • flutter怎么封装点击菜单工具栏组件checkBox多选版
    这篇“flutter怎么封装点击菜单工具栏组件checkBox多选版”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“flutt...
    99+
    2023-06-30
  • PyQt5中怎么创建菜单栏和工具栏
    在PyQt5中可以使用QMenuBar来创建菜单栏,使用QToolBar来创建工具栏。下面是一个简单的例子: import sys ...
    99+
    2024-03-12
    PyQt5
  • Vue3 Element-plus和el-menu无限级菜单组件怎么封装
    对于element中提供给我们的el-menu组件最多可以实现三层嵌套,如果多一层数据只能自己通过变量去加一层,如果加了两层、三层这种往往是行不通的,所以只能进行封装效果图 一、定义数据MenuData.tsexport default [...
    99+
    2023-05-14
    Vue3 Element-plus el-menu
  • win10菜单栏与工具栏颜色和壁纸怎么匹配颜色?
    升级win10后,相信大具栏颜无法改变,所以小编今天为大家带来方便快速修改的经验 第一步,打开菜单设置 第二步,打开里面的个性化 打开颜色 将显示菜单、任务栏和操作中心,设置设定为“开&rdq ...
    99+
    2023-06-17
    win10菜单栏打不开 win10工具栏 win10窗口颜色 win10标题栏颜色 win10 工具栏 壁纸 菜单栏 颜色
  • Flutter怎么实现单选,复选和开关组件
    本文小编为大家详细介绍“Flutter怎么实现单选,复选和开关组件”,内容详细,步骤清晰,细节处理妥当,希望这篇“Flutter怎么实现单选,复选和开关组件”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1、开关 ...
    99+
    2023-06-30
  • vue中怎么实现二级菜单导航点击选中事件
    这篇文章将为大家详细讲解有关vue中怎么实现二级菜单导航点击选中事件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。目标:一级导航点击收缩展开,二级导航点击选...
    99+
    2024-04-02
  • 怎么使用vue3 element-plus二次封装组件制作伸缩菜单
    本篇内容主要讲解“怎么使用vue3 element-plus二次封装组件制作伸缩菜单”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用vue3 element-plus二...
    99+
    2023-07-05
  • vue3怎么封装input组件和统一表单数据
    准备工作用vue create example创建项目,参数大概如下:用原生 input原生的 input,主要是 value 和 change,数据在 change 的时候需要同步。App.tsx如下:import { ref } fro...
    99+
    2023-05-14
    Vue3 input
  • 怎么使用Springboot封装一个自适配的数据单位转换工具类
    今天小编给大家分享一下怎么使用Springboot封装一个自适配的数据单位转换工具类的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一...
    99+
    2023-07-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作