Open In App

Flutter – Empty Widget

Empty widget is the flutter widget that is mainly used to notify the user about some event, Like if we are fetching the data from the API or From the database, There may be a case when API returns the null at that moment we can show the empty widget. Also If there is no user internet connection we can notify the user by this widget.

How to Use

Use the Empty widget in the body of the scaffold or child of the container, Give parameters title, subtitle, image, etc.






EmptyWidget(
   // image from the project
   image: null,
   // image from the package assets
   packageImage: PackageImage.Image_3,
   title: 'going to be title here',
   subTitle: 'Subtitle here',
   // styling the text
   titleTextStyle: TextStyle(
      fontSize: 22,
     color: Color(0xff9da9c4),
     fontWeight: FontWeight.bold,
   ),
   // styling the sub title
   subtitleTextStyle: TextStyle(
     fontSize: 14,
     // color of text
     color: Color(0xffabb8e5),
   ),
 );

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.



Step 2: Import the Package in the pubspec.yaml File

Now we need to import the package into the pubspec.yaml file, which you find at the last of the root folder. From the command line:




flutter pub add  empty_widget

This will add the following code in the pubspec.yaml file-

 

Step 3: Import the package into the main file




import 'package:empty_widget/empty_widget.dart';

Properties:

Parameter

Type

Description

title string   Set text for title
subTitle  string  Set text for subtitle
image  string  Display images from project assets
packageImage PackageImage    Display images from package assets
titleTextStyle 
 
TextStyle  Set text style for title
subtitleTextStyle 
 
TextStyle  Set text style for subtitle
hideBackgroundAnimation  bool  Hides the background circular ball animation

Code Example




import 'package:empty_widget/empty_widget.dart';
import 'package:flutter/material.dart';
 
void main() => runApp(MyApp());
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp( 
      title: 'Empty Widget',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text('Empty widget Flutter'),
        ),
        body: Container(
          alignment: Alignment.center,
          child: EmptyWidget(
            image: null,
            packageImage: PackageImage.Image_2 ,
            title: 'No Data',
            subTitle: 'No  Data available yet',
            titleTextStyle: TextStyle(
              fontSize: 22,
              color: Color.fromARGB(255, 31, 150, 47),
              fontWeight: FontWeight.w500,
            ),
            subtitleTextStyle: TextStyle(
              fontSize: 14,
              color: Color.fromARGB(255, 21, 226, 199),
            ),
          ),
        ),
      ),
    );
  }
}

Code Explanation

Output

 

Using Assets Image

For using assets image, you have to add the asset to your project, See this article on how to add asset images in Flutter. Now in the empty widget in the property image give the path of the image.

 

Hide the Animation

You can enable or disable animation using hideBackgroundAnimation property of the empty widget. This property accepts the true or false value. The default value is false.

 


Article Tags :