Unable to load asset: lib/assets/background.jpg - flutter

Why is it i cant load background image in my project? Unable to load asset: lib/assets/background.jpg
import 'package:flutter/material.dart';
Widget testWidget = new MediaQuery(
data: new MediaQueryData(), child: new MaterialApp(home: new MyApp()));
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("lib/assets/background.jpg"),
fit: BoxFit.cover,
),
),
child: null /* add child content here */,
),
);
}
}
file tree
pubspec.yaml
assets:
- lib/assets/background.jpg

Do not make3 folder under lib folder. Your structure for assets folder as per below.
And pubspecs.yaml

I will suggest to remove this code:
Widget testWidget = new MediaQuery(
data: new MediaQueryData(), child: new MaterialApp(home: new MyApp()));
Move assets folder to the root of the project and update the path in pubspec.yaml
assets:
- /assets/
Change MyApp to use MaterialApp and HomePage:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
Then move the background image code to the HomePage inside the Scaffold:
Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/background.jpg'),
fit: BoxFit.cover,
),
),
),
),

if your assets included into androin folder...try this
flutter:
assets:
-android/assests/directory/

Related

Dart Flutter display Icons

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.

image doesn't display on the app even though it was in assets folder

I added the image file on assets folder.
but when I run the app, image doesn't display at all.
and there isn't any error message.
I don't know the reason why I cannot see the picture I added.
(of course, it isn't the white picture. It isthe normal picture.)
Please let me know the reason.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Center(
child: GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HeroDetailPage()),
);
},
child: Hero(
tag: 'image',
child: Image.asset(
'assets/sample.jpg',
width: 100,
height: 100,
),
),
),
),
);
}
}
class HeroDetailPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hero Detail'),
),
body: Hero(
tag: 'image',
child: Image.asset('assets/sample.jpg'),
),
);
}
}
Please check: https://flutter.dev/docs/development/ui/assets-and-images first.
There you can find this:
Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.
Here is an example:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
To include all assets under a directory, specify the directory name with the / character at the end:
flutter:
assets:
- directory/
- directory/subdirectory/
To add assets to your application, add an assets section, like this in your pubspec.yaml file:
assets:
- assets/
First add your image file in pubspec.yml file
assets:
- assets/sample.jpg

Flutter: How to add a zoom functionality to image

In my flutter app, a custom image is built with the following code
customImage.dart
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
Widget cachedNetworkImage(mediaUrl) {
return CachedNetworkImage(
imageUrl: mediaUrl,
fit: BoxFit.cover,
placeholder: (context, url)=>
Padding(padding: EdgeInsets.all(20.0),
child: CircularProgressIndicator(),
),
errorWidget: (context, url, error) => Icon(Icons.error) ,
);
}
I want to add a functionality to pinch to zoom on this image,how can I achieve that?
You can copy paste run full code below
You can use Flutter's bulid-in InteractiveViewer https://api.flutter.dev/flutter/widgets/InteractiveViewer-class.html
A widget that enables pan and zoom interactions with its child.
working demo
full code
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
Widget cachedNetworkImage(mediaUrl) {
return CachedNetworkImage(
imageUrl: mediaUrl,
fit: BoxFit.cover,
placeholder: (context, url) => Padding(
padding: EdgeInsets.all(20.0),
child: CircularProgressIndicator(),
),
errorWidget: (context, url, error) => Icon(Icons.error),
);
}
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatelessWidget(),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Center(
child: InteractiveViewer(
boundaryMargin: EdgeInsets.all(20.0),
minScale: 0.1,
maxScale: 1.6,
child: cachedNetworkImage("https://picsum.photos/250?image=9"),
),
);
}
}
Use photo_view for this. It allows you to create zoomable image widgets without dealing with pinch gesture or sth.
import 'package:photo_view/photo_view.dart';
#override
Widget build(BuildContext context) {
return Container(
child: PhotoView(
imageProvider: AssetImage("assets/image.jpg"),
)
);
}
Source: https://pub.dev/packages/photo_view

when i run the code,i didnot see my image,just appears white screen on device,why?

void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Row(
children: <Widget>[
Expanded(
child: Image.asset('images/dice1.png'),
),
],
),
),
);
}
}
In your pubspec.yaml add the assets:
flutter:
assets:
- images/dice1.png
Make sure the directory is correct, more info here:
https://flutter.dev/docs/development/ui/assets-and-images
You have to return runapp or you can use an arrow instead.
void main() => runApp(MyApp());

Can not render image in flutter Splash screen/First Screen(iOS only)

I am adding an background image to the Splash screen, but is not rendering the image. Sometime it load sometime not.If I load in the Second screen it loads properly. The issue is in iOS not in Android.
Here is a sample code:
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
//runApp()
// SplashWidget
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: UIData.buildTheme(),
home: SplashS(),
));
}
class SplashS extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(body:Container(
color: Colors.yellow,
child: Center(
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child:Image.asset(
"images/image_name.png",
fit: BoxFit.fill,
)),
),
));
}
}
Have you included the image path in your pubspec.yaml?
assets:
- images/image_name.png