Flutter Image Widget Tutorial

Certainly! For creating cross-platform mobile applications, Flutter is a well-liked framework, and the Image widget is frequently used to display photos in your app.

In this article, I’ll show you how to display images using Flutter’s Image widget and give you an example you can use in your blog post.

Setting up Flutter

If you haven’t already, you’ll need to set up Flutter on your machine. You can follow the official Flutter installation guide

Adding Images

Place your images in the assets directory of your Flutter project. You should create an assets directory in the root of your project if it doesn’t exist. For this example, let’s assume you have an image named sample_image.jpg inside the assets directory.

Open the pubspec.yaml file in your project and add the following lines to declare the asset:

flutter:
  assets:
    - assets/sample_image.jpg

Using the Image Widget:

Open the lib/main.dart file in your project. Replace the default MyApp widget with the following code to display the image using the Image widget:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Widget Example'),
      ),
      body: Center(
        child: Image.asset('assets/sample_image.jpg'),
      ),
    );
  }
}

You can now write your blog post, describing how to use the Image widget in Flutter. Include explanations of the steps above, along with any additional information you think would be helpful for your readers.

The Image widget in Flutter comes with several properties that allow you to customize how the image is displayed. Here are some commonly used properties along with examples:

fit Property:

Image.asset(
  'assets/sample_image.jpg',
  fit: BoxFit.cover, // This will cover the entire space while maintaining aspect ratio.
)

width and height Properties:

Image.asset(
  'assets/sample_image.jpg',
  width: 200,
  height: 150,
)

alignment Property:

Image.asset(
  'assets/sample_image.jpg',
  alignment: Alignment.center, // Aligns the image at the center of its space.
)

color and colorBlendMode Properties:

You can apply a color filter to the image using the color property and control how the colors are blended using colorBlendMode.

Image.asset(
  'assets/sample_image.jpg',
  color: Colors.blue, // Applies a blue color filter.
  colorBlendMode: BlendMode.darken, // Specifies how colors are blended.
)

repeat Property:

Image.asset(
  'assets/sample_image.jpg',
  repeat: ImageRepeat.repeat, // Repeats the image along both axes.
)

frameBuilder Property:

Image.asset(
  'assets/sample_image.jpg',
  frameBuilder: (BuildContext context, Widget child, int frame, bool wasSynchronouslyLoaded) {
    if (wasSynchronouslyLoaded) {
      return child; // Return the image if it was loaded synchronously.
    }
    return CircularProgressIndicator(); // Display a progress indicator while loading.
  },
)

Remember to customize the example and explanations to suit your writing style and the specific focus of your blog post. You can also add more features, styling, or interactions to your app to make the example more engaging and informative.

Related Posts

Flutter Icon Widget Tutorials

Flutter GridView Tutorial

Flutter Webview Tutorial

Mastering FlatButton in Flutter

ElevatedButton Tutorial for Flutter

Numeric String Validation in Flutter

Tabbar with image in flutter

Good day, readers A few weeks ago, one of the readers who left a comment on my story requested that I write a piece regarding the TabBar and TabBarView widgets.

So here it is: in addition to addressing these two widgets, we will also speak about the DefaultTabBarController and TabController.

If you dont know how to implement basic Tabbar in Flutter you can check this post of the Simple Tabbar in Flutter.

You might concur with me that designing a tabbed layout in Android is more difficult than it is in Flutter if you are an Android developer.

The basic Syntax of the Tabbar is given below:

TabBar(           
  indicatorColor: Colors.white,
  indicatorWeight: 2.0,
  indicatorPadding: EdgeInsets.zero,
  indicatorSize: TabBarIndicatorSize.label,
  indicator: BoxDecoration(),
  labelColor: Colors.white,
  labelStyle: TextStyle(),
  labelPadding: EdgeInsets.only(top: 10.0),
  unselectedLabelColor: Colors.grey,
  unselectedLabelStyle: TextStyle(),
  isScrollable: false,
  controller: _tabController,  // only  if DefaultTabContoller is not used.
  dragStartBehavior: DragStartBehavior.start,
  mouseCursor: SystemMouseCursors.noDrop,
  physics: ScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
  onTap: (){},
  tabs: [Tab(),]                 // @required list of Tabs

Here is the example of Image on Tabbar in Flutter Implementation.

Open the lib/main.dart file in your project directory and replace the code with the following:

class TabbarDisplay extends StatefulWidget {
  @override
  TabbarDisplayState createState() => TabbarDisplayState();
}

class TabbarDisplayState extends State<tabbardisplay> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3, // Number of tabs
        child: Scaffold(
          appBar: AppBar(
            title: Text('Tech Android Hub'),
            bottom: TabBar(
              tabs: [
                Tab(
                  icon: Icon(Icons.add), // Image or Icon you can add 
                  text: 'Tab 1',
                ),
                Tab(
                  icon: Icon(Icons.access_alarm),
                  text: 'Tab 2',
                ),
                Tab(
                  icon: Icon(Icons.ac_unit_rounded),
                  text: 'Tab 3',
                ),
              ],
            ),
          ),
          body: TabBarView(
            children: [
              Center(child: Icon(Icons.add),),
              Center(child: Icon(Icons.access_alarm)),
              Center(child: Icon(Icons.ac_unit_rounded)),
            ],
          ),
        ),
      ),
    );
  }
}</tabbardisplay>

By using the icon attribute, we have modified the previous code in this example to add Icon widgets to each Tab. Alternatively, if you like, you can use a custom icon or any other icon from the Icons class.

The tabbed interface should now appear, with three tabs and an image on each one. Each tab’s associated image will appear in the centre of the screen when it is touched.

For creating complex user interfaces, Flutter offers a flexible and user-friendly framework. You are welcome to try different things, like as changing the graphics on the tabs or including new features in the app.

Related Posts

Simple Tabbar in Flutter

Progress Bars Example in Flutter

Creating a Splash Screen in Flutter

Remove DEBUG Banner in the Flutter app

Rounded Corners Image in Flutter