欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Flutter仿京东商城APP底部导航实现

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

2024/10/24 6:45:49 来源:https://blog.csdn.net/qq_37703224/article/details/143173709  浏览:    关键词: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: "我的"),]),);}
}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com