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.
Dart
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),
),
);
- image - Specify the image to be displayed on the screen. this image will be loaded from assets.
- packageImage - You can specify pre-defined images if you don't want to use images from assets.
- title - Text to display below the image.
- subtitle - Text to display below the title.
- Animation - You can enable or disable animation using the hideBackgroundAnimation property of the empty widget. This property accepts the true or false value. The default value is false.
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:
Dart
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
Dart
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
Dart
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
- main is a principal method called once the program is loaded.
- Once the Program Is Loaded runApp Will Run And Call The Class That We Created (MyApp).
- This Class Is StatelessWidget Because No Change to Do.
- This Class Is Taking MaterialApp Only.
- Flutter is Based On Widgets So A Widget must be Built.
- Creating MaterialApp (Placed One Time In The Whole Project) That Define Application Settings (debugShowCheckedModeBanner , title ... ).
- Home Is Taking widget Scaffold.
- Returning Scaffold That Allows Us To Set Body and AppBar Of The Page.
- AppBar Is Taking AppBar That Is Taking Only Text as a Title.
- As We Need to center the widget We Put a container with an alignment center.
- Now We Need To Create an Empty Widget.
- Empty widget allows us to set the title, sub-title, and image.
- We also styles the text using the titleStyleText and subTitleStyleText.
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.
Similar Reads
Flutter - Chip Widget
Chip is a material design widget which comes built-in with flutter. It can simply be described as a compact element holding an icon and text, usually a rounded rectangle in the background. It can serve many purposes, like it can be simply used as a button, representing a user with a CircleAvatar and
4 min read
Flutter - Expanded Widget
Expanded widget in flutter comes in handy when we want a child widget or children widgets to take all the available space along the main-axis (for Row the main axis is horizontal & vertical for Column). Â Expanded widget can be taken as the child of Row, Column, and Flex. And in case if we don't
5 min read
Flutter - Banner Widget
Banner widget comes built-in with flutter API. It is somewhat similar to the debug banner that we are used to seeing on the top-right corner on a flutter app in debug mode. It enables us to show a message or text on top of any other widget. Below we will see its implementation with the help of an ex
3 min read
Flutter - Border Widget
Border widget in flutter is assigned a simple functionality to add borders to the other widgets. The first is by creating all borders using BorderSide. The second way is by using Border.all to create a uniform border having the same color and width. The third is by using Border.fromBorderSide to cre
4 min read
Flutter - Card Widget
Card is a built-in widget in Flutter which derives its design from Google's Material Design Library. The functionality of this widget on screen is, that it is a bland space or panel with round corners and a slight elevation on the lower side. It comes with many properties like color, shape, shadow c
4 min read
Center widget in Flutter
Center widget comes built-in with flutter, it aligns its child widget to the center of the available space on the screen. The size of this widget will be as big as possible if the widthFactor and heightFactor properties are set to null and the dimensions are constrained. And in case the dimensions a
2 min read
Flutter - Autocomplete Widget
In this article, we will learn how to develop an autocomplete feature in Flutter that allows users to quickly search and select items from a large set of data. To demonstrate this feature, we will walk through a demo program step-by-step. This functionality can be achieved using the autocomplete wid
4 min read
Flutter - Custom Widgets
We create Custom Widgets when we want a custom look and feel to our app, and we know that there will be a repetition of a particular widget. We can create the custom widget in a new dart file with all the codes and defining the parameters that we need in the constructor. For more on how to split wid
8 min read
Flutter - Flexible Widget
Flexible is a built-in widget in flutter which controls how a child of base flex widgets that are Row, Column, and Flex will fill the space available to it. The Expanded widget in flutter is shorthand of Flexible with the default fit of FlexFit.tight. Flexible widget plays a very important part in m
8 min read
ClipRect Widget in Flutter
The ClipRect widget is used to clips its child using a rectangle. It associates with the Clippers family. The main use of clippers is to clip out any portion of the widget as required. It behaves similar to that of ClipRRect and is used to Clip a Rectangle portion of the child widget but without the
2 min read