How can i make transparent appBar in flutter [duplicate] - flutter

This question already has answers here:
How can we change appbar background color in flutter
(11 answers)
Closed 2 years ago.
Now I want to make transparent appBar
So I looked for a way, but the examples put appBar on top of the image, but I want to put appBar on top of the widget, not on the image.
I want to know the way to make transparent appBar on top of the widget not image.
Here is my code
// main.dart
void main() => runApp(Main());
class Main extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SideBarLayout(),
);
}
}
// SideBarLayout.dart
class SideBarLayout extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
....
],
child: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Help'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(30),
),
),
),
body: Stack(
children: <Widget>[
Consumer<MenuItem>(
builder: (BuildContext context, MenuItem value, Widget child){
if(value.menuType == MenuType.pomodoro) { return Pomodoro(); }
else if (value.menuType == MenuType.calendar) { return MonthTable(); }
else if (value.menuType == MenuType.todoList) { return TodoList(); }
else if (value.menuType == MenuType.settings) { return SettingView(); }}),
SideBar(),
],
),
),
);
}
}

I would set the backgroundColor of your AppBar to Colors.transparent and elevation to zero as well as setting extendBodyBehindAppBar: true, at your Scaffold.
Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
...
),
body: ...,
)

The AppBar uses the primary color of the Theme object of your MaterialApp.
You can also change the backgroundColor property and set it to Colors.transparent.
Check the first answer!

Related

Status bar color not changing when it is set in the AnnotatedRegion

Minimal reproducible code:
class MyPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
int count = 0;
Widget child = FooPage();
return StatefulBuilder(
builder: (_, update) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => update(() => child = (++count).isEven ? FooPage() : BarPage()),
),
body: child,
);
},
);
}
}
class FooPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return AnnotatedRegion(
value: SystemUiOverlayStyle(
statusBarColor: Colors.black,
systemNavigationBarColor: Colors.black,
),
child: Scaffold(
appBar: AppBar(title: Text('Foo')),
),
);
}
}
class BarPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return AnnotatedRegion(
value: SystemUiOverlayStyle(
statusBarColor: Colors.grey,
systemNavigationBarColor: Colors.grey,
),
child: Scaffold(
appBar: AppBar(title: Text('Bar')),
),
);
}
}
As you can see, the navigation bar color changes but the status bar color stays the same. Why is it so? I am testing this on Android.
The two widgets are meant to be used in this way (not through the Navigator).
Use this
AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.white),
child: ----
}
Widget build(BuildContext context) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
systemNavigationBarColor: Colors.orange
),
child: Scaffold(
appBar: AppBar(title: Text('Foo'),systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.red,
)),
body: Container(),
),
);
}

Navigation bar color not changing when it is set in the AppBar

Minimal reproducible code:
class MyPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
int count = 0;
Widget child = FooPage();
return StatefulBuilder(
builder: (_, update) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => update(() => child = (++count).isEven ? FooPage() : BarPage()),
),
body: child,
);
},
);
}
}
class FooPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Foo'),
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.black,
systemNavigationBarColor: Colors.black,
),
),
);
}
}
class BarPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Bar'),
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.grey,
systemNavigationBarColor: Colors.grey,
),
),
);
}
}
As you can see, the status bar color changes but the navigation bar color stays the same. Why is it so? I am testing this on Android.
The two widgets are meant to be used in this way (not through the Navigator).
its better put your code in main method above of runApp() and run gain
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(systemNavigationBarColor: Colors.black));

How to change appbar when listened to Bloc, I dont want to bloc builder on the scaffold, instead I want BlocBuidler on the AppBar?

Scaffold(
// extendBodyBehindAppBar: true,
// extendBody: true,
appBar: AppBar(
centerTitle: false,
brightness: Brightness.light,
leading: IconButton(
icon: const Icon(
Icons.arrow_back,
),
onPressed: () {
Navigator.pop(context);
}),
title: const Text(
'Go back',
),
elevation: 0,
backgroundColor: Colors.transparent,
),
.
.
.
bottomNavigationBar: BottomNavigationWidget()
)//Scaffold;
Simply, I want to change my app bar when the bottom navigation item is changed.. I am unable to wrap AppBar with the BlocBuilder<>, How can I achieve this?
Inside Scaffold widget you can use
appBar: PreferredSize(
child: CustomAppBar(),
preferredSize: Size.fromHeight(56),
),
And create another function or class as CustomAppBar which returns AppBar
return AppBar(
title: Text(
text ?? "Default Text",
),
);
And pass the text as parameter and if it is null you can set some default text for it.
The best solution is to wrap your AppBar with a BlocBuilder and wrap your BlocBuilder with Preferred Size. credit: jurej1 on GitHub.
appBar: PreferredSize(
preferredSize: const Size.fromHeight(56),
child: BlocBuilder<TurfBloc, TurfState>(
builder: (context, state) {
return AppBar(
centerTitle: true,
title: Text(state.text),
backgroundColor: state.color,
);
},
),
),
You can't, as Scaffold appBar property accept widgets that extend PreferredSizeWidget class. Wrap Scaffold with BlocBuilder and depending on Bloc state just change AppBar properties:
return BlocBuilder<YourBloc, YourState>(
builder: (context, state) => Scaffold(
appBar: AppBar(
title: state is Selected1 ? Text('Title1') : Text('TitleElse'),
elevation: state is Selected1 ? 0 : 1
)
)
);
Eventually create new widget that extend PreferredSizeWidget:
class MyAppBar extends StatelessWidget with PreferredSizeWidget {
#override
Widget build(BuildContext context) {
return BlocBuilder<YourBloc, YourState>(
builder: (context, state) {
if(state is Selected1) {
return AppBar(...);
} else if (state is Selected2) {
return AppBar(...);
} else {
return AppBar(...);
}
}
);
}
#override
Size get preferredSize => Size(appbarwidth, appbarheight);
}
and use it, but be sure you have BlocProvider<YourBloc> above Scaffold:
return Scaffold(appBar: MyAppBar());

How to put AppBar just under status bar?

I want to put AppBar just under status bar, but I couldn't, what can I do?
I tried primary:true, or SafeArea, my code as below:
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("ホーム"),
),
body: Center(child: Text("ホーム")),
);
}
}
I also tried code from answer, but same result.
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("ホーム"),
),
body: Center(child: Text("ホーム")),
));
}
}
You should try the SafeArea. But you must wrap the entire Scaffold inside the SafeArea.
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("ホーム"),
),
body: Center(child: Text("ホーム")),
)
);
}
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
var sbHeight = MediaQuery.of(context).padding.top;
return Container(
margin: EdgeInsets.only(top: sbHeight),
child: Scaffold(
appBar: AppBar(
title: Text("Title"),
centerTitle: true,
),
body: Center(child: Text(sbHeight.toString())),
),
);
}
}
You can use more about SafeArea here: SafeArea

why does mediaquery.of() error keep showing up?

I am just trying to create an simple widget but it keeps showing this error what should I do?
It looks like your root widget is not MaterialApp, other widgets that you are using need to be descendants of some media query ancestor. Eg. MaterialApp.
Instead of this:
Widget build(BuildContext context) {
return Container(
child: SomeWidget(),
);
}
Make it:
Widget build(BuildContext context) {
return MaterialApp(
home: SomeWidget(),
);
}
You are getting the error because your application doesn't return a Material App. Check this code below, it works perfectly.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text('unit converter', textScaleFactor: 1.0,),
),
body: Container(
color: Colors.purpleAccent,
),
),
);
}
}
The output of the code:
You are getting this error because on the top of the tree a material widget should be used.
here you can use Scaffold or MaterialApp widget.
and If you are trying to paint your screen background to some color then just use the parameter backgroundColor under your Scaffold widget.
Try code shown below
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.purpleAccent,
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text("unit converter",textScaleFactor:4.0)
),
body: yourWidget()
);
}