How to add an image to the Project?

This Article is posted by seven.srikanth at 8/27/2018 1:36:25 PM




Follow the below steps to add images to the Flutter project.

Create a Folder call images or with any other name and add images over there. And then you need to update in the pubspec.yaml file. WIthout adding the details to pubspec.yaml file, you cannot utilize those images.  

name: flutterimagesexamples
description: A new Flutter project.

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2

dev_dependencies:
flutter_test:
sdk: flutter

flutter:
uses-material-design: true
assets:
- images/carimg.jpeg


and then in main.dart page add the below code

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Image Examples'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);


final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/carimg.jpeg',
width: 600.0,
height: 240.0,
fit: BoxFit.cover,)
],
),
),
);
}
}


Thanks,
Srikanth


Tags: Add images to Flutter Project;








0 Comments
Login to comment.
Recent Comments












© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com