篇首语:本文由编程笔记#小编为大家整理,主要介绍了Flutter学习笔记--仿闲鱼底部导航栏带有中间凸起图标相关的知识,希望对你有一定的参考价值。
霸图镇楼
需求情景: 需要实现一个类似闲鱼APP的底部导航栏的实现
Demo地址 : https://github.com/hanlin19900610/flutter_bottom_navigation_bar
import 'package:flutter/material.dart';
import 'pages/MainPage.dart';
void main() => runApp(LightLanguageClient());
class LightLanguageClient extends StatelessWidget{
@override Widget build(BuildContext context) {
return MaterialApp( title: 'Demo', theme: ThemeData( primarySwatch: Colors.blue ), home: MainPage(), ); } }
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget{
@override State
return _HomePageState(); } }
class _HomePageState extends State<HomePage>{
@override Widget build(BuildContext context) {
return MaterialApp( home: Scaffold( body: Center( child: Text('这是首页'), ), ), ); } }
这个三个页面的代码都一样就没有都贴出来
import 'package:flutter/material.dart';
import 'HomePage.dart';import 'AssistantPage.dart';
import 'MinePage.dart';
class MainPage extends StatefulWidget {
@override
State
return _MainPage();
}
}
class _MainPage extends State<MainPage> with SingleTickerProviderStateMixin {
PageController pageController;
int page = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: new PageView(
children:
controller: pageController,
onPageChanged: onPageChanged,
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
BottomNavigationBarItem(icon: Icon(Icons.assessment), title: Text('助手')),
BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('我的')),
],
onTap: onTap,
currentIndex: page,
),
));
}
@override
void initState() {
super.initState();
pageCOntroller= PageController(initialPage: this.page);
}
@override
void dispose() {
super.dispose();
pageController.dispose();
}
void onTap(int index) {
pageController.animateToPage(index,
duration: const Duration(milliseconds: 300), curve: Curves.ease);
}
void onPageChanged(int page) {
setState(() {
this.page = page;
});
}
}
items: 添加底部导航栏的每个Item
onTap: 为底部导航栏设置点击事件
currentIndex: 为底部导航设置当前选中项
class _MainPage extends State<MainPage> with SingleTickerProviderStateMixin {
PageController pageController;
int page = 0; //添加图片地址,需要动态更换图片
String bigImg = 'images/home_green.png';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: new PageView(
children:
controller: pageController,
onPageChanged: onPageChanged,
), //重构bottomNavigationBar
bottomNavigationBar: Stack(
children:
Align(
alignment: Alignment.bottomCenter,
child: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
BottomNavigationBarItem(icon: Icon(Icons.assessment), title: Text('')),
BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('我的')),
],
onTap: onTap,
currentIndex: page,
),
),
Align(
alignment: Alignment.bottomCenter,
child: InkWell(
child: new Image.asset(bigImg,width: 80.0,height: 80.0,),
onTap:onBigImgTap,
),
)
],
)
));
}
@override
void initState() {
super.initState();
pageCOntroller= PageController(initialPage: this.page);
}
@override
void dispose() {
super.dispose();
pageController.dispose();
}
//修改bottomNavigationBar的点击事件
void onTap(int index) {
if (index != 1) {
setState(() {
this.bigImg = 'images/home_green.png';
});
}
pageController.animateToPage(index,
duration: const Duration(milliseconds: 300), curve: Curves.ease);
}
//添加图片的点击事件
void onBigImgTap() {
setState(() {
this.page = 1;
this.bigImg = 'images/icon_home.png';
onTap(1);
});
}
void onPageChanged(int page) {
setState(() {
this.page = page;
});
}
}
重构完成之后,效果图如下,我们发现这并不是我们想要的,底部导航栏我们是实现了,但是PageView被遮盖了
bottomNavigationBar: Container(
height: 100.0,
child: Stack(
children:
Align(
alignment: Alignment.bottomCenter,
child: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('首页')),
BottomNavigationBarItem(
icon: Icon(Icons.accessibility_new), title: Text('')),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text('我的')),
],
onTap: onTap,
currentIndex: page,
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: InkWell(
child: new Image.asset(
bigImg,
width: 80.0,
height: 80.0,
),
onTap: onBigImgTap,
),
)),
],
),
)
physics: NeverScrollableScrollPhysics(),
解决方案很简单,Android原生的白屏问题可以通过为 Launcher Activity 设置 windowBackground 解决,而 Flutter 也是基于此办法,同时优化了 Flutter 初始化阶段的白屏问题(覆盖一个launchView),只用两步设置便能解决 Flutter 中白屏问题。
在项目的 android/app/src/main/res/mipmap-xhdpi/ 目录下添加闪屏图片;
打开 android/app/src/main/res/drawable/launch_background.xml 文件,这个文件就是闪屏的背景文件,具体如何设置可以查阅 Android Drawable,我在 demo 中的设置如下:
"1.0" encoding="utf-8"?>
Demo地址 : https://github.com/hanlin19900610/flutter_bottom_navigation_bar
刚开始接触Flutter,代码写的有些混乱,可能有些问题考虑不是很深入,有不足之处,还请各位大佬指出
扫一扫
点击阅读原文,了解更多精彩内容