Flutter沉迷式透明状态栏|flutter自己设置凸起BottomAppBar导航

  • 时间:2020-11-08 03:16 作者:Android架构骆驼 来源: 阅读:537
  • 扫一扫,手机访问
摘要:原作者:xiaoyan2017 原文链接:人类身份验证 SegmentFault来源:SegmentFault 思否前言如下图:状态栏是指android手机顶部显示手机状态信息的位置。android 自4.4开始新加入透明状态栏功能,状态栏可以自己设置颜色背景,使titleBar能够和状态栏融为一

原作者:xiaoyan2017 原文链接:人类身份验证 - SegmentFault
来源:SegmentFault 思否

前言

如下图:状态栏是指android手机顶部显示手机状态信息的位置。

android 自4.4开始新加入透明状态栏功能,状态栏可以自己设置颜色背景,使titleBar能够和状态栏融为一体,添加沉迷感。

如上图:Flutter状态栏默认为黑色半透明,那么如何去掉这个状态栏的黑色半透明背景色,让其和标题栏颜色一致,通栏沉迷式,实现如下图效果呢?

首先需要在flutter项目目录下找到android主入口页面MainActivity.kt或者MainActivity.java,判断一下版本号而后将状态栏颜色修改设置成透明,由于他本身是黑色半透明。

在MainActivity.kt页面新添加如下代码

//设置状态栏沉迷式透明(修改flutter状态栏黑色半透明为全透明)override fun onCreate(savedInstanceState: Bundle?) {    super.onCreate(savedInstanceState);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        window.statusBarColor = 0    }}

完整MainActivity.kt代码如下:

package com.example.flutter_appimport androidx.annotation.NonNull;import io.flutter.embedding.android.FlutterActivityimport io.flutter.embedding.engine.FlutterEngineimport io.flutter.plugins.GeneratedPluginRegistrant//引入import android.os.Build;import android.os.Bundle;class MainActivity: FlutterActivity() {    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {        GeneratedPluginRegistrant.registerWith(flutterEngine);    }    //设置状态栏沉迷式透明(修改flutter状态栏黑色半透明为全透明)    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            window.statusBarColor = 0        }    }}

注意:flutter项目默认是使用Kotlin语言

在Google I/O 2017中,Google 宣布 Kotlin 取代 Java 成为 Android 官方开发语言。

Kotlin介绍见:https://www.kotlincn.net/

通过 flutter create flutter_app 命令创立flutter项目时,默认是Kotlin语言模式,假如想要修改成Java语言,则运行如下命令创立项目就可

flutter create -a java flutter_app

假如是java语言模式下,修改沉迷式状态栏方法和上面同理

MainActivity.java路径:
android\app\src\main\java\com\example\flutter_app\MainActivity.java

在MainActivity.java页面新添加如下代码

package com.example.demo1;import androidx.annotation.NonNull;import io.flutter.embedding.android.FlutterActivity;import io.flutter.embedding.engine.FlutterEngine;import io.flutter.plugins.GeneratedPluginRegistrant;// 引入import android.os.Build;import android.os.Bundle;public class MainActivity extends FlutterActivity {  @Override  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {    GeneratedPluginRegistrant.registerWith(flutterEngine);  }  // 设置状态栏沉迷式透明(修改flutter状态栏黑色半透明为全透明)  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {      getWindow().setStatusBarColor(0);    }  }}

Flutter实现咸鱼底部导航凸起效果

如下图: BottomNavigationBar 组件仿咸鱼凸起导航栏配置

int _selectedIndex = 0;// 创立数组引入页面List pglist = [HomePage(), FindPage(), CartPage(), ZonePage(), UcenterPage(),];...Scaffold(    body: pglist[_selectedIndex],        // 抽屉菜单    // drawer: new Drawer(),    // 普通底部导航栏    bottomNavigationBar: BottomNavigationBar(        fixedColor: Colors.red,        type: BottomNavigationBarType.fixed,        elevation: 5.0,        unselectedFontSize: 12.0,        selectedFontSize: 18.0,        items: [            BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),            BottomNavigationBarItem(icon: Icon(Icons.search), title: Text('Find')),            BottomNavigationBarItem(icon: Icon(null), title: Text('Cart')),            BottomNavigationBarItem(icon: Icon(Icons.photo_filter), title: Text('Zone')),            BottomNavigationBarItem(icon: Icon(Icons.face), title: Text('Ucenter')),        ],        currentIndex: _selectedIndex,        onTap: _onItemTapped,    ),        floatingActionButton: FloatingActionButton(        backgroundColor: _selectedIndex == 2 ? Colors.red : Colors.grey,        child: Column(            mainAxisAlignment: MainAxisAlignment.center,            children: [                Icon(Icons.add)            ]        ),        onPressed: (){            setState(() {                _selectedIndex = 2;            });        },    ),    floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,)void _onItemTapped(int index) {    setState(() {        _selectedIndex = index;    });}

如下图: BottomAppBar 组件凸起凹陷导航栏配置

int _selectedIndex = 0;// 创立数组引入页面List pglist = [HomePage(), FindPage(), CartPage(), ZonePage(), UcenterPage(),];...Scaffold(    body: pglist[_selectedIndex],        // 抽屉菜单    // drawer: new Drawer(),    // 底部凸起凹陷导航栏    bottomNavigationBar: BottomAppBar(        color: Colors.white,        shape: CircularNotchedRectangle(),        child: Row(            mainAxisAlignment: MainAxisAlignment.spaceAround,            children: <Widget>[                IconButton(                    icon: Icon(Icons.home),                    color: _selectedIndex == 0 ? Colors.red : Colors.grey,                    onPressed: (){                        _onItemTapped(0);                    },                ),                IconButton(                    icon: Icon(Icons.search),                    color: _selectedIndex == 1 ? Colors.red : Colors.grey,                    onPressed: (){                        _onItemTapped(1);                    },                ),                                SizedBox(width: 50,),                                IconButton(                    icon: Icon(Icons.photo_filter),                    color: _selectedIndex == 3 ? Colors.red : Colors.grey,                    onPressed: (){                        _onItemTapped(3);                    },                ),                IconButton(                    icon: Icon(Icons.face),                    color: _selectedIndex == 4 ? Colors.red : Colors.grey,                    onPressed: (){                        _onItemTapped(4);                    },                ),            ],        ),    ),)void _onItemTapped(int index) {    setState(() {        _selectedIndex = index;    });}

基于flutter实现沉迷式状态栏+凸起导航栏就分享到这里,希望能有些帮助。💪💪

GitHub

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】【Azure API Management】实目前API Management服务中使用MI(管理标识 Managed Identity)访问启用防火墙的Storage Account(2025-10-16 23:53)
【系统环境|】【Azure 存储服务】App Service 访问开启防火墙的存储账号时遇见 403 (This request is not authorized to perform this opera...(2025-10-16 23:52)
【系统环境|】YouTube账号购买全攻略|点击查看原文购买|购买YouTube账号、油管号自动发货服务的优势与缺点 附:YouTube无法加载?可能是这个设置在作怪!(2025-10-16 23:52)
【系统环境|】【Azure Developer】Azure Automation 自动化账号生成的时候怎么生成连接 与证书 (Connection & Certificate)(2025-10-16 23:41)
【系统环境|】【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置(2025-10-16 23:40)
【系统环境|】近日,四川宜宾,某地下停车场内的监控拍到了一幕惊险的画面。危险!一汽车修理师傅正在修理一辆3吨重的悍马时,悍马车突然自己启动,眼看就要撞向前面的一辆灰色轿车了,一侧的修理师傅做出了一个令人始料不...(2025-10-16 23:38)
【系统环境|】iOS 能耗监控与电池优化实战:如何查看App耗电量、分析CPU、GPU内存使用、(uni-app iOS开发性能调试指南)(2025-10-16 23:37)
【系统环境|】《风流女管家》法国犯罪悬疑片--《风流女管家》法语高清全集免费在线无广告完整观看---《风流女管家》HD正版免费播放-手机电脑皆可看(2025-10-16 23:36)
【系统环境|】2025含金量排名前十计算机专业证书(2025-10-15 20:51)
【系统环境|】你有白帽众测 我有黑帽雇佣(2025-10-15 20:50)
手机二维码手机访问领取大礼包
返回顶部