flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

xiaoyan2015發表於2020-04-27

  • Flutter中如何實現通欄全屏沉浸式狀態列效果?

flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

如上圖:狀態列是指android手機頂部顯示手機狀態資訊的位置。 android 自4.4開始新加入透明狀態列功能,狀態列可以自定義顏色背景,使titleBar能夠和狀態列融為一體,增加沉浸感。

Flutter建立專案執行後狀態列預設為黑色半透明,那麼如何去掉這個狀態列的黑色半透明背景色,讓其和標題欄顏色一致,通欄沉浸式,實現如下圖效果呢?

flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

flutter專案目錄下找到android主入口頁面MainActivity.kt或MainActivity.java,判斷一下版本號然後將狀態列顏色修改設定成透明,因為他本身是黑色半透明。

flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

在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_app

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;

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詳情見: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實現鹹魚底部導航凸起效果

flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

如上圖: 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;
    });
}
複製程式碼

flutter全屏沉浸式狀態列+標題欄|flutter凸起Tabbar導航

如上圖: 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實現沉浸式狀態列就分享到這裡,希望能對大家有些幫助!??

最後分享個Vue例項專案:vue2.x全家桶仿微信聊天例項


相關文章