I have a global variable that is true if using Android. The code below doesn't work. How can I decide what widget to display depending on the platform:
rmoMenu is a custom widget that returns a Drawer widget
rmoBottomNavigationBar is a custom widget that returns a BottomNavigationBar widget
#override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
appBar: rmoAppBar(subText: 'My Shifts'),
if (globals.gblIsAndroid == true) then {
drawer: rmoMenu()
} else {
bottomNavigationBar: rmoBottomNavigationBar(),
},
body: ...
You can use the ternary operator:
Scaffold(
appBar: rmoAppBar(subText: 'My Shifts'),
drawer: globals.gblIsAndroid ? rmoMenu() : null,
bottomNavigationBar: !globals.gblIsAndroid ? rmoBottomNavigationBar() : null,
Related
When I have an AppBar on my screen, it looks like this:
But I don't need AppBar so I removed it. As a result my screen looks like this:
As you can see, the top of my screen is in the StatusBar (where there are clock, Internet connection icon, and so on).
Please help me change my code so that my screen starts right after the StatusBar.
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Container(
child: Scaffold(
// appBar: AppBar (title: const Text(_title)),
body: const MainWidget(),
),
)
);
}
Thanks in advance.
Edit 1. Why doesn't this work for the Container case? I had this code on another screen:
#override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height,
...
I changed it to this:
#override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
height: MediaQuery.of(context).size.height,
...
But nothing has changed. Why?
Wrap your scaffold body with SafeArea widget
#override
Widget build(BuildContext context) {
return SafeArea(
For your case, do it on home or scaffold.
home: SafeArea(
More about SafeArea
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(MyApp.title),
centerTitle: true,
),
body: (()=>{
if(!list){
return Register();
}
else{
return Homepage();
}
})
);
My application fetches a list from local storage on init afterwards the next page to be rendered depends on whether the list is empty or not. Is there a way to determine which widget to render base on a condition?
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(MyApp.title),
centerTitle: true,
),
body: list.isEmpty ? Register() : Homepage()
);
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
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!
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()
);
}