flutter手写一个底部导航栏

使用了一下flutter提供的导航栏,NavigationBar,不过感觉使用起来不是很方便。
譬如说:

  1. 不能直接使用图片资源,需要中间加几层转换把图片转化成icon
  2. 文本大小及颜色不太好控制
  3. 状态栏的上边来一个横线也没有相应的样式,等等。

因此想来想去还是自己写一个吧。
效果如下:

在这里插入图片描述
控件代码如下:

import 'package:flutter/material.dart';
import 'dart:developer' as developer;class XXNaviIcon {XXNaviIcon({required this.normalImage,required this.selectedImage,required this.title});final String selectedImage;final String normalImage;final String title;
}class XXNaviBar extends StatefulWidget {const XXNaviBar({super.key, required this.icons, this.onDestinationSelected}): currentIndex = 0;final List<XXNaviIcon> icons;final ValueChanged<int>? onDestinationSelected;final int currentIndex;State<XXNaviBar> createState() => _XXNaviBarState();
}class _XXNaviBarState extends State<XXNaviBar> {int currentIndex = 0;Widget build(BuildContext context) {return SafeArea(child: SizedBox(height: 48,child: Container(decoration: const BoxDecoration(border:Border(top: BorderSide(width: 0.5, color: Color(0xffbbbbbb)))),child: Row(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[for (int i = 0; i < widget.icons.length; i++)Expanded(child: GestureDetector(onTap: () => {developer.log("tap $i"),setState(() {currentIndex = i;}),if (widget.onDestinationSelected != null){widget.onDestinationSelected!(i)}},child: Container(color: Colors.white,child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Image.asset(currentIndex == i? widget.icons[i].selectedImage: widget.icons[i].normalImage,height: 24,),Text(widget.icons[i].title,style: const TextStyle(fontSize: 10, fontWeight: FontWeight.bold),),],),),))],),),));}
}

使用方式如下:

Widget build(BuildContext context) {return Scaffold(backgroundColor: Colors.white,bottomNavigationBar: XXNaviBar(onDestinationSelected: (value) => {developer.log("click tab $value"),if (currentPageIndex != value){setState(() {currentPageIndex = value;})}},icons: [XXNaviIcon(normalImage: "assets/images/tabbar/home.png",selectedImage: "assets/images/tabbar/home-selected.png",title: "首页",),XXNaviIcon(normalImage: "assets/images/tabbar/data.png",selectedImage: "assets/images/tabbar/data-selected.png",title: "数据",),XXNaviIcon(normalImage: "assets/images/tabbar/mine.png",selectedImage: "assets/images/tabbar/mine-selected.png",title: "我的",),],),body: <Widget>[Container(color: Colors.white,alignment: Alignment.center,child: const Text('Page 1'),),Container(color: Colors.white,alignment: Alignment.center,child: const Text('Page 2'),),Container(color: Colors.white,alignment: Alignment.center,child: const Text('Page 3'),),][currentPageIndex],);}

定制化可以根据自己的实际需求自行更改。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/7752.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【云原生 | 53】Docker三剑客之Docker Compose应用案例一:Web负载均衡

&#x1f341;博主简介&#xff1a; &#x1f3c5;云计算领域优质创作者 &#x1f3c5;2022年CSDN新星计划python赛道第一名 &#x1f3c5;2022年CSDN原力计划优质作者 &#x1f3c5;阿里云ACE认证高级工程师 &#x1f3c5;阿里云开发者社区专…

Django纪录操作之增删改查

一、单表 1、 添加记录 准备表 from django.db import modelsclass Book(models.Model):title models.CharField(max_length20)price models.DecimalField(max_digits65,decimal_places5)publish models.CharField(max_length30)pub_date models.DateTimeField(auto_now…

Python财经股票数据获取, 保存表格文件

目录标题 前言环境使用:模块使用]:代码展示尾语 前言 嗨喽~大家好呀&#xff0c;这里是魔王呐 ❤ ~! 环境使用: Python 3.8 解释器 Pycharm 编辑器 模块使用]: import requests —> 数据请求模块 pip install requests import csv 第三方模块安装: win R 输入cmd 输…

大数据Doris(五十):Export导出原理

文章目录 Export导出原理 一、原理 二、查询计划拆分 三、查询计划执行 Export导出原理 Doris Export、Select Into Outfile、MySQL dump三种方式数据导出。用户可以根据自己的需求导出数据。此外数据还可以以文件形式通过Borker备份到远端存储系统中&#xff0c;之后可以…

【Linux】Linux项目自动化构建工具-make/makefile

Linux项目自动化构建工具-make/makefile 什么是make/makefile&#xff1f;make/makefile的使用依赖关系依赖方法makefile是如何工作的&#xff1f;为什么要使用makefile呢&#xff1f;makefile是怎么做到的呢&#xff1f;make和make clean.PHONY&#xff1a;伪目标 特殊符号&am…

1 Prometheus-监控简介

目录 1 什么是监控 1.1 技术作为客户 1.2 业务作为客户 2. 监控基础知识 2.1 事后监控 2.2 机械式/模板式/无脑式监控 2.3 不够准确的监控 2.4 静态监控 2.5 不频繁的监控 2.6 缺少自动化或操作繁琐/不便 2.7 监控模式总结 3.监控机制 3.1 探针和内省 3.2 拉取和推…

LangChain大型语言模型(LLM)应用开发(二):Conversation Memory

LangChain是一个基于大语言模型&#xff08;如ChatGPT&#xff09;用于构建端到端语言模型应用的 Python 框架。它提供了一套工具、组件和接口&#xff0c;可简化创建由大型语言模型 (LLM) 和聊天模型提供支持的应用程序的过程。LangChain 可以轻松管理与语言模型的交互&#x…

Unity协程

unity提供了一种类似“多段代码并行执行”的功能&#xff0c;即协程。 我们在定义一个协程的时候&#xff0c;需要遵循类似这样的语法 IEnumerator&#xff08;枚举器接口&#xff09; namespace System.Collections {public interface IEnumerator{object Current { get; }/…

MySql基础知识及数据查询

目录 第一章 数据库概述 1.为什么要学习数据库&#xff1f; 2.数据库的相关概念 3.ORM(Object Relational Mapping)思想 4.表与表的记录之间存在哪些关联关系 第二章 基本的SELECT语句 1.SQL的分类 2. SQL基本规则 3.导入现有的数据表、表的数据 4.最基本的…

位图的详解

目录 位图 位图的概念 位图的实现 位图常见三道面试题 1.给定100亿个整数&#xff0c;设计算法找到只出现一次的整数&#xff1f; 2. 给两个文件&#xff0c;分别有100亿个整数&#xff0c;我们只有1G内存&#xff0c;如何找到两个文件交集&#xff1f; 3. 位图应用变形…

使用随便测测平台-做接口测试

目录 接口数据的来源 导出har文件 导入har文件 转化为用例 提取数据进行替换 如何选择哪些数据需要替换呢&#xff1f; Url Params、Data ​编辑进行替换操作 断言-冒烟 断言-详细 测试报告 结束 接口数据的来源 1、可通过charles工具&#xff0c;录制好接口操作&…

IDEA恢复误删除的文件

idea将删除的文件放在idea文件缓存中&#xff0c;文件的更改等信息都放在这个缓存中&#xff0c;所以短时间内删除的文件可以尝试恢复。