Flutter
  • Flutter Tutorials Handbook
  • 01 Flutter: Hello World
    • Related Article
  • 02 Flutter: Stateless Widgets
  • 03 Flutter: Buttons and Stateful widgets
  • 04 Flutter: Basic Navigation & Routes
    • Simple routes using StatelessWidget
    • Routes using StatefulWidget
  • 05 Flutter: Using onChanged to show input text
  • 06 Flutter: Using onSubmitted to show input text after submit
  • 07 Flutter: Adding-Deleting text in TextField
  • 08 Flutter: Tab Navigation
  • 09 Flutter: HTTP requests and Rest API
  • 10 Flutter: ListView with JSON or List Data
  • 11 Flutter: Sliding menu using a Drawer
    • Simple Drawer
    • Drawer using variables
  • 12 Flutter: Animations
  • 13 Flutter: JSON Storage
  • 14 Flutter: Friendly Chat App
  • 15 Flutter: Changing icon color onfocus
  • 16 Flutter: Horizontal ListView and Tabs
  • 17 Flutter: RaisedButton
  • 18 Flutter: RaisedButton with parameters
  • 19 Flutter: FlatButton
  • 20 Flutter: IconButton
  • 21 Flutter: Updating data in TextField
  • 22 Flutter: Checkbox
  • 23 Flutter: Radio with Functions
  • 24 Flutter: Switch
  • 25 Flutter: Slider
  • 26 Flutter: DatePicker
  • 27 Flutter: appBar
  • 28 Flutter: Floating Action Button
  • 29 Flutter: Drawer which shows Toggle Menu
  • 30 Flutter: Footer Buttons
  • 31 Flutter: BottomNavigationBar
  • 32 Flutter: Using BottomSheets
  • 33 Flutter: Using SnackBar
  • 34 Flutter: AlertDialog
  • 35 Flutter: SimpleDialog
  • 36 Flutter: Rows and Columns
  • 37 Flutter: Using Cards
  • 38 Flutter: Using Expanded
  • 39 Flutter: ListviewBuilder using Dart maps
  • 40 Flutter: ListviewBuilder using Dart lists
  • 41 Flutter: Using Custom Widgets
  • 42 Flutter: Using Image Assets
  • 43 Flutter: ListviewBuilder with Refresh Indicator
Powered by GitBook
On this page

38 Flutter: Using Expanded

main.dart
import 'package:flutter/material.dart';
import 'dart:async';

void main(){
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => new _State();
}

//State is information of the application that can change over time or when some actions are taken.
class _State extends State<MyApp>{

  /*
  1. Create a directory called images and add your image there
  2. In pubspec.yamp add dependency for using assets
  */

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Name here'),
        backgroundColor: Colors.red,
      ),
      //hit Ctrl+space in intellij to know what are the options you can use in flutter widgets
      body: new Container(
        padding: new EdgeInsets.all(32.0),
        child: new Center(
          child: new Column(
            children: <Widget>[
              new Text('Image from assets'),
              new Image.asset('images/flutter.png'),
              new Text('Image from network'),
              //this image was outside the screen so we are using Expanded which will fit the image into the screen
              new Expanded(child: new Image.network('https://static.makeuseof.com/wp-content/uploads/2012/10/flutter-logo.jpg'))
            ],
          ),
        ),
      ),
    );
  }
}

We've created images directory in our Flutter app to store our flutter.png used in this project and defined that in pubspec.yaml file

pubspec.yaml
name: flutter_udemy_course
description: A new Flutter project.

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - images/flutter.png
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages
Previous37 Flutter: Using CardsNext39 Flutter: ListviewBuilder using Dart maps

Last updated 6 years ago