Created
August 7, 2019 13:25
-
-
Save slightfoot/d6372b458e915ead9b16f50f4fa72aec to your computer and use it in GitHub Desktop.
Transparent Navigation in Flutter on Android.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.Window; | |
import io.flutter.app.FlutterActivity; | |
import io.flutter.plugins.GeneratedPluginRegistrant; | |
public class MainActivity extends FlutterActivity | |
{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
GeneratedPluginRegistrant.registerWith(this); | |
drawUnderSystemUi(); | |
} | |
@Override | |
protected void onPostResume() | |
{ | |
super.onPostResume(); | |
drawUnderSystemUi(); | |
} | |
private void drawUnderSystemUi() | |
{ | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ | |
Window window = getWindow(); | |
int color; | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ | |
color = Color.TRANSPARENT; | |
} | |
else{ | |
color = 0x22000000; | |
} | |
window.setNavigationBarColor(color); | |
window.getDecorView().setSystemUiVisibility( | |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | | |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | | |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui' as ui; | |
import 'package:flutter/foundation.dart' show defaultTargetPlatform; | |
import 'package:flutter/material.dart'; | |
class YourApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Your app', | |
theme: ThemeData.dark(), | |
builder: _supportTransparentNavOnAndroid, | |
home: HomeScreen(), | |
); | |
} | |
Widget _supportTransparentNavOnAndroid(BuildContext context, Widget child) { | |
if (defaultTargetPlatform == TargetPlatform.android) { | |
final mediaQuery = MediaQueryData.fromWindow(ui.window); | |
final bottomInset = mediaQuery.viewInsets.bottom; | |
return MediaQuery( | |
data: mediaQuery.copyWith( | |
padding: mediaQuery.padding.copyWith( | |
bottom: (bottomInset < 64.0 ? bottomInset : 0.0), | |
), | |
viewInsets: mediaQuery.viewInsets.copyWith( | |
bottom: (bottomInset < 64.0 ? 0.0 : bottomInset), | |
), | |
), | |
child: child, | |
); | |
} else { | |
return child; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment