Working with Text and Images in Flutter

Working with Text and Images in Flutter

Introduction

Flutter is Google’s open-source UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. Working with text and images is fundamental in almost every Flutter app. This guide covers how to effectively display, style, and manage text and images in Flutter applications.


1. Displaying Text

Flutter provides the Text widget to display strings with various styles.


Basic Example

dart

Copy

Edit

Text('Hello, Flutter!');

Styling Text

Use the style property with TextStyle:


dart

Copy

Edit

Text(

  'Styled Text',

  style: TextStyle(

    fontSize: 24,

    fontWeight: FontWeight.bold,

    color: Colors.blue,

    letterSpacing: 2,

  ),

);

Text Alignment and Overflow

dart

Copy

Edit

Text(

  'This is a long piece of text that might overflow.',

  textAlign: TextAlign.center,

  overflow: TextOverflow.ellipsis,

  maxLines: 1,

);

2. Working with Rich Text

Use RichText and TextSpan for multiple styles in one block of text.


dart

Copy

Edit

RichText(

  text: TextSpan(

    text: 'Flutter ',

    style: TextStyle(color: Colors.black, fontSize: 18),

    children: <TextSpan>[

      TextSpan(text: 'is ', style: TextStyle(fontWeight: FontWeight.normal)),

      TextSpan(text: 'awesome!', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue)),

    ],

  ),

);

3. Displaying Images

Flutter provides the Image widget to display images from various sources.


From Assets

Add to pubspec.yaml:


yaml

Copy

Edit

flutter:

  assets:

    - assets/images/sample.png

Use in code:


dart

Copy

Edit

Image.asset('assets/images/sample.png');

From Network

dart

Copy

Edit

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

From File (Local Storage)

dart

Copy

Edit

Image.file(File('path/to/image.png'));

From Memory (Uint8List)

dart

Copy

Edit

Image.memory(bytes);

4. Image Properties and Styling

Resize and Fit


dart

Copy

Edit

Image.asset(

  'assets/images/sample.png',

  width: 100,

  height: 100,

  fit: BoxFit.cover,

);

Add Border Radius


dart

Copy

Edit

ClipRRect(

  borderRadius: BorderRadius.circular(10),

  child: Image.asset('assets/images/sample.png'),

);

Use Placeholder with FadeInImage


dart

Copy

Edit

FadeInImage.assetNetwork(

  placeholder: 'assets/images/placeholder.png',

  image: 'https://example.com/image.png',

);

5. Combining Text and Images

Use Row, Column, or Stack to layout text and images together.


dart

Copy

Edit

Row(

  children: [

    Image.asset('assets/images/icon.png', width: 40),

    SizedBox(width: 10),

    Text('Image with text'),

  ],

);

6. Performance and Best Practices

Use const where possible for static text and images.


Use CachedNetworkImage package to cache images:


yaml

Copy

Edit

dependencies:

  cached_network_image: ^3.2.3

dart

Copy

Edit

CachedNetworkImage(

  imageUrl: 'https://example.com/image.png',

  placeholder: (context, url) => CircularProgressIndicator(),

  errorWidget: (context, url, error) => Icon(Icons.error),

);

Optimize images for mobile use: compress assets, use WebP or JPEG.


Conclusion

Flutter makes it simple to work with both text and images using intuitive and flexible widgets. Mastering these basic elements is essential for creating beautiful, responsive, and performant Flutter apps.

Learn Flutter Training in Hyderabad

Read More

How to Use the Flutter Debug Console

Hot Reload vs Hot Restart in Flutter

Dart Basics for Flutter Developers

Exploring the Flutter Directory Structure

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions


 

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Tosca for API Testing: A Step-by-Step Tutorial

Working with Tosca Parameters (Buffer, Dynamic Expressions)