Working with images and fonts

Working with images and fonts is a common requirement when developing Flutter applications. In this guide, we'll explain how to work with images and custom fonts in a Flutter project.

Working with Images:

  1. Adding Images to Your Project:

    • Create a directory (e.g., assets/) in your Flutter project's root folder.

    • Place your image files (e.g., PNG, JPG) inside this directory.

  2. Update pubspec.yaml to Include Assets:

    • Open the pubspec.yaml file.

    • Add an assets section to specify the images you want to include. For example:

    flutter:
      assets:
        - assets/image1.png
        - assets/image2.png
  3. Displaying Images:

    • To display an image, you can use the Image widget or Image.asset() constructor.

    • Example using Image.asset():

    Image.asset('assets/image1.png')
  4. Network Images:

    • For network images, use the Image.network() constructor.

    Image.network('https://example.com/image.jpg')

Working with Custom Fonts:

  1. Adding Custom Fonts:

    • Create a fonts/ directory in your Flutter project's root folder.

    • Place your custom font files (e.g., .ttf, .otf) inside this directory.

  2. Update pubspec.yaml to Include Fonts:

    • Open the pubspec.yaml file.

    • Add a fonts section to specify the fonts you want to include. For example:

    flutter:
      fonts:
        - family: MyCustomFont
          fonts:
            - asset: fonts/my_custom_font.ttf
  3. Using Custom Fonts:

    • To use a custom font, specify the fontFamily property in text widgets.

    Text(
      'Hello, Custom Font!',
      style: TextStyle(
        fontFamily: 'MyCustomFont',
        fontSize: 20.0,
      ),
    )
  4. Loading Fonts Dynamically (Optional):

    • If you want to load fonts dynamically, use the pub package to fetch fonts on-demand.

Additional Notes:

  • Hot Reload: You can use Flutter's hot reload feature to see changes in images and fonts instantly during development.

  • Remember to add your assets and fonts to the pubspec.yaml file, and run flutter pub get to ensure they are included in your project.

  • When using custom fonts, make sure the font family name matches what you specified in pubspec.yaml.

Working with images and custom fonts in Flutter is straightforward, and it allows you to create visually appealing and customized user interfaces for your mobile apps.

Last updated