当前位置: 首页 > article >正文

Flutter仿京东商城APP底部导航实现

01 基础代码

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'jdshop',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: '京东商城'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Text("我是首页"),
    );
  }
}

02 配置底部导航

核心代码

bottomNavigationBar: BottomNavigationBar(items: const [
  BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
  BottomNavigationBarItem(icon: Icon(Icons.category), label: "分类"),
])

完整代码

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'jdshop',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: '京东商城'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Text("我是首页"),
      bottomNavigationBar: BottomNavigationBar(items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
        BottomNavigationBarItem(icon: Icon(Icons.category), label: "分类"),
      ]),
    );
  }
}

03 抽离底部导航代码

main.dart

import 'package:flutter/material.dart';
import 'pages/tabs/tabs.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'jdshop',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const Tabs(title: "京东商城"),
    );
  }
}

pages/tabs/tabs.dart

import 'package:flutter/material.dart';

class Tabs extends StatefulWidget {
  const Tabs({super.key, required this.title});

  final String title; // 标题

  
  TabsState createState() => TabsState();
}

class TabsState extends State<Tabs> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Text("我是首页"),
      bottomNavigationBar: BottomNavigationBar(items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
        BottomNavigationBarItem(icon: Icon(Icons.category), label: "分类"),
      ]),
    );
  }
}

04 增加底部导航选项

核心代码

1.添加索引

int _currentIndex = 0;

2.配置底部导航

bottomNavigationBar: BottomNavigationBar(
  currentIndex: _currentIndex,
  onTap: (index) {
    setState(() {
      _currentIndex = index;
    });
  },
  type: BottomNavigationBarType.fixed, // 超过2个必须配置这个才显示
  items: const [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
    BottomNavigationBarItem(icon: Icon(Icons.category), label: "分类"),
    BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), label: "购物车"),
    BottomNavigationBarItem(icon: Icon(Icons.people), label: "我的"),
  ]),
);

完整代码

pages/tabs/tabs.dart

import 'package:flutter/material.dart';

class Tabs extends StatefulWidget {
  const Tabs({super.key, required this.title});

  final String title; // 标题

  
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  int _currentIndex = 0;

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Text("我是首页"),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed, // 超过2个必须配置这个才显示
          items: const [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
            BottomNavigationBarItem(icon: Icon(Icons.category), label: "分类"),
            BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), label: "购物车"),
            BottomNavigationBarItem(icon: Icon(Icons.people), label: "我的"),
          ]),
    );
  }
}

完整代码或者录播课或者私教课请私信我


http://www.kler.cn/news/363178.html

相关文章:

  • 音质最好的麦克风有哪些?领夹麦克风哪个品牌好?麦克风十大品牌
  • 阿里云项目启动OOM问题解决
  • Git使用GUI界面实现任意历史版本对比
  • 界面控件DevExtreme中文教程 - 如何与Amazon S3和Azure Blob存储集成?
  • AnaTraf | 网络性能监控系统NPM:提升网络性能与业务连续性
  • 使用飞桨AI Studio平台训练数据,并进行图像识别分析得牡丹花测试
  • 代码随想录训练营Day06 | 454.四数相加II - 383. 赎金信 - 15. 三数之和 - 18. 四数之和
  • 毕业设计选题:基于django+vue的个人博客系统设计与开发
  • Github 2024-10-17 Go开源项目日报 Top10
  • 华为Eth-Trunk级联堆叠接入IPTV网络部署案例
  • 网络安全:数字世界的护卫
  • 访问kerberos认证华为的kafka集群
  • 【Python爬虫实战】使用BeautifulSoup和Scrapy抓取网页数据!
  • 大模型进阶微调篇(二):基于人类反馈的强化学习RLHF原理、优点介绍,但需要警惕LLMs的拍马屁行为
  • 2000-2023年上市公司绿色专利申请授权面板数据
  • 最近本地vmware workstation虚拟机用了一段时间后就出现网络很慢,登录不了的现象
  • 教育技术的未来:Spring Boot在线教学平台
  • SwitchHosts快速修改host文件
  • 数据资产入表:政策与实践全面解读
  • lego-loam featureAssociation 源码注释(二)
  • 嵌入式-ftrace
  • 【微信小程序_18_WXS脚本】
  • CSS学习(Grid布局和flex布局比较)
  • SDK下载依赖到IDEA的详细指南
  • ctfshow-文件上传-151-161
  • 三大智能体平台深度对比:字节Coze、百度AppBuilder、智谱智能体优劣解析