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
Related
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(),
),
);
}
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));
I created several screens, for some reasons I have to individually create a Scaffold which represents the screen. However, as the AppBar should be everytime the same, I thought of create it once in a stateless widget and the just reuse this:
import 'package:flutter/material.dart';
class MyAppBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return AppBar(
centerTitle: true,
backgroundColor: Colors.black,
title: Text(
"Places Near You",
style: TextStyle(
color: Colors.black, fontFamily: "Billabong", fontSize: 35),
),
);
}
}
and then on every Screen i wanting to use this by writting:
class _CreatePostScreenState extends State<CreatePostScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: MyAppBar(),
body: Center(
child: Text("Hello"),
));
}
}
However, I get the following error which I dont know how to solve (I imported everything correctly):
Your app bar must implement PreferredSizeWidget.
class YourAppbar extends StatelessWidget implements PreferredSizeWidget {
#override
Widget build(BuildContext context) {
return AppBar();
}
#override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
For me the following solution went fine too:
class YourAppBar extends AppBar {
YourAppBar({Key key, Widget title})
: super(key: key, title: title, actions: <Widget>[
new IconButton(
onPressed: (){},
icon: new Icon(Icons.access_alarm)),
]);
}
And can be used as follow:
return Scaffold(
appBar: YourAppBar(
title: Text('Hi'),
),
body: Center(
child: Text('Home page'),
),
);
I am learning flutter. i have made one app name chat x(title). the code is below
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Chat X'),
),
body: Card(child: Column(children: <Widget>[
Text('hey! this one is new');
],),),
),
);
}
}
when ever i want to debug this code i get this error
Expected to find ']'.
Remove ; from Text('hey! this one is new'); or you can place a , if you want to have more widgets inside Column
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Chat X'),
),
body: Card(child: Column(children: <Widget>[
Text('hey! this one is new'),
Text('hey! this one is second new'),
],),),
),
);
}
}
You shoud not have the ; after the Text widget. Like this:
Text('hey! this one is new')
I want to make a new route in the flutter , but I failed,
my VS Code give me this:
The following assertion was thrown while handling a gesture:
I/flutter (32582): Navigator operation requested with a context that does not include a Navigator.
I/flutter (32582): The context used to push or pop routes from the Navigator must be that of a widget that is a
I/flutter (32582): descendant of a Navigator widget
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 new MaterialApp(
title: 'Lake',
routes: {
'sss': (context)=>new NewRoute()
},
home: new Scaffold(
appBar: AppBar(
title: Text('Lake'),
),
body: Text('BBB'),
floatingActionButton: new FloatingActionButton(
child: Icon(Icons.import_contacts),
onPressed: (){
Navigator.pushNamed(context, 'sss');
},
),
),
);
}
}
class NewRoute extends StatelessWidget{
#override
Widget build(BuildContext context){
return new Scaffold(
appBar: AppBar(
title: Text('BBB'),
),
body: Center(
child: Text('wahaha'),
),
);
}
}
Plea use this code
home: Builder(
builder: (context) => Scaffold(
appBar: AppBar(
title: Text('Lake'),
),
body: Text('BBB'),
floatingActionButton: new FloatingActionButton(
child: Icon(Icons.import_contacts),
onPressed: (){
Navigator.pushNamed(context, 'sss');
},
),
),)
Builder let you build a new context from direct parent like described there https://docs.flutter.io/flutter/widgets/Builder-class.html
You are using routes incorrectly. when you use home in MaterialApp it will bypass the Routes. insted of that you can use initialRoute to define Home Screen
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(
title: 'Lake',
routes: {
'sss': (context) => const NewRoute(),
'home': (context) => const HomeScreen(),
},
initialRoute: 'home',
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Lake'),
),
body: const Text('BBB'),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.import_contacts),
onPressed: () {
Navigator.pushNamed(context, 'sss');
},
),
);
}
}
class NewRoute extends StatelessWidget {
const NewRoute({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BBB'),
),
body: const Center(
child: Text('wahaha'),
),
);
}
}
also avoid using new keyword.
you can also try with onGenarated Routes in flutter.
more information