I am new in a flutter. Kindly don't mind if this question is weird. I need a background image in AppBar. I have found on the internet and got the solution with a widget SliverAppBar. I need the image background image in normal appear. I found there is no property of image or background image in AppBar. Can anyone help me with this?
Thanks! Darshan.
welcome to stackoverflow.
I have made this demo earlier for understanding. Kindly follow the below example code.
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(App());
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: AppBarWithImage(),
);
}
}
class AppBarWithImage extends StatefulWidget {
#override
_AppBarWithImageState createState() => _AppBarWithImageState();
}
class _AppBarWithImageState extends State<AppBarWithImage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('App Bar with image background'),
flexibleSpace: Image(
image: NetworkImage(
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350",
),
fit: BoxFit.cover,
),
backgroundColor: Colors.transparent,
),
body: Center(
child: Text("Sample Text"),
),
);
}
}
Output:
Conclusion
the property called "flexibleSpace" is what you are looking for the background image.
Related
Im writing my first code and I want to display a Icon, and I try to use the command Icon (Icons.star, color: Colors.red, size: 100, ),. But it doesn't work. Can somebody help me. Thx
Welcome to Stackoverflow.
Try the following code. Your above code is also correct.
Try to check below line in your pubspec.yaml file
flutter:
uses-material-design: true
Refer Flutter Icons
Full Example:
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Icon(
Icons.star,
color: Colors.red,
size: 100,
),
);
}
}
Result screen->
You can also test the code on Dartpad.
I have an application (simplified) that looks like this:
working dartpad example:
https://dartpad.dev/cc4e524315e104c272cd06aa7037aa10
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: MyWidget(),
),
bottomNavigationBar: BottomWidget(),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Text('Hello, World!');
}
}
class BottomWidget extends StatelessWidget {
Widget build(BuildContext context) {
return BottomAppBar(
child: InkWell (
child: Container(
height:100,
color: Colors.pink,
child: Center(
child: Text('this is a bottom navigation bar')
),
),
onTap: () {
showModalBottomSheet(
context: context,
builder: (context) => Container(),
);
}
),
);
}
}
My question is, I want to use showModalBottomSheet to stop user from interacting with the screen, but I need bottomNavigationBar to be shown like this:
Right now it just covers everything including the bottomNavigationBar:
If I change it to showBottomSheet, it works perfectly like the first picture.
Why is their behaviour different?
How can I achieve the result that I want using showModalBottomSheet?
showModalBottomSheet show bottom sheet on top of all widget and layers, if you want to show it under bottomNavigationBar you should use Stack in parent and use a single child into that. its not easy to take a sample and that take a long time to reproduce pseudo code
I am using the following code for coloring the status bar in flutter.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(statusBarColor: Colors.blue),
child: MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue),
home: MyHomeScreen(),
navigatorKey: navigatorKey,
),
);
}}
class MyHomeScreen extends StatelessWidget {
const MyHomeScreen({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Header(), // my own custom header
),
);
}
}
With the above code I am getting the desired result in Android but in iOS the status bar is still showing in white color.
I have tried this link but I am not getting the desired solution for iOS.
I had also same issue. i found solution with this.
try that package.
Add the following lines to your code ,
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(statusBarColor: any Color),
);
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
It'll change the whole app's status bar color :)
UPDATE:
you can use this trick to achieve status bar color in Ios
#override
Widget build(BuildContext context) {
return Container(
color: any color,
child: SafeArea(
child: Scaffold(
body: Header(), // my own custom header
),
),
);
}
I have a Home page for an ecommerce website and I am showing every product in image format on click of every product(image) I want that image to be filled up in next screen which is of info screen of product that also contains the product (image) and some other related stuff. I want to build info page only a single time and just change the product image and other stuff accordingly as per the image (product) being clicked. How can I achieve this I am new to flutter and dont know this logic i came across valuelistenablebuilder but not getting how can i achieve this, Please Help.
Thank you,
By default, Flutter is providing this support by using the Hero widget. Find the code snippets below.
import 'package:flutter/material.dart';
void main() => runApp(HeroApp());
class HeroApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Transition Demo',
home: MainScreen(),
);
}
}
class MainScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main Screen'),
),
body: GestureDetector(
child: Hero(
tag: 'imageHero',
child: Image.network(
'https://picsum.photos/250?image=9',
),
),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen();
}));
},
),
);
}
}
class DetailScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
child: Center(
child: Hero(
tag: 'imageHero',
child: Image.network(
'https://picsum.photos/250?image=9',
),
),
),
onTap: () {
Navigator.pop(context);
},
),
);
}
}
Make sure the Hero.tag property should be maintained as same for both pages.
Refer the link for more details
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()
);
}