Zoom Webinar: Learn and enhance your TypeScript skills | 28th March 2023 | 10 AM PST [10:30 PM IST] Register here
Education

Flutter Widgets Cheatsheet: Categories, Types and Basic Widgets

logo

DhiWise

March 3, 2023
image
Author
logo

DhiWise

{
March 3, 2023
}

Flutter is Google’s UI toolkit for crafting beautiful, natively compiled iOS and Android apps using a single code base. The Flutter app is designed using widgets – These are the basic building blocks of every Flutter application. Widgets describe what the app UI should look like given its current configuration and state.

There are multiple Flutter widgets, the articles give you a quick look into the basic Flutter widgets, types, and categories. 

Flutter has become a star in the eyes of Android and iOS developers by empowering them to build cross-platform apps using a single codebase. The technology seems to be successfully overcoming the three big challenges in app development. 

  • There is no need to have separate teams for Android and iOS development, just hire one team and implement the cross-platform development functionality of Flutter. 
  • Improves productivity and speeds up development with Dart’s “Just in Time” and “Ahead of  Time” compilation. And thus it helps to reduce the development cost to half.
  • Unlike other cross-platform options, Flutter makes the app more beautiful, stable, and consistent. 

All that brings much relief for the developer's community as they now have more time for creative things in app development. 

Well, now we know how useful Flutter is for smoothly building beautiful cross-platform apps. Let's know about the basic building blocks of the Flutter application known as Flutter widgets. 

Flutter Widgets: 

Each element on the Flutter UI is a widget such as text, container, image, button, animations, and motions. So, the view of a screen completely depends on the choice and the sequence of the Flutter widgets used in building the application. Therefore the structure formed by the application code is called a tree of widgets. 

These widgets can be classified into the following fourteen  categories according to the functionality they provide in the Flutter application.

Category Description
Accessibility Makes the app more accessible.
Animation and motions Add animation to the widgets.
Assets image and icon Manage assets, display images, and show icons.
Async Provides async functionality to the Flutter application.
Basics Essential widgets for the Flutter application development.
Cupertino Beautiful and high-fidelity widgets for iOS styling.
Input Take user input in addition to input widgets in Material Components and Cupertino.
Interaction Models Respond to touch events and route users to different views.
Layout Arranges other widgets, columns, rows, grids, and other layouts.
Material Components Visual, behavioral, and motion-rich widgets that follow material design guidelines.
Painting and Effects Set of widgets that add visual changes to their child widgets without changing their layout and shape.
Scrolling Scrolls multiple widgets as the children of the parents.
Styling It deals with the theme, responsiveness, and sizing of the app.
Text It displays and styles text.

Flutter Widget types

In general Flutter widgets are classified into two categories,

  1. Stateless Widget
  2. Stateful Widget

However, you can find more classes in the hierarchy of Flutter sources

soruce
  • StatelessWidget: The widget does not require a mutable state. 
  • StatefulWidget: The widget has a mutable state i.e state information can be read synchronously when it is built and it may change during the widget's lifetime. Therefore it's essential to ensure that the state is promptly notified when the change occurs using setState.   
  • ProxyWidget: The Widget has a child widget provided to it, instead of building a new widget. It is useful as a base class for other widgets like InheritedWidget and ParentDataWidget. 
  • RenderObjectWidget: It provides the configuration for the RenderObjectElements, which wrap RenderObjects, that provide the actual rendering of the application.
  • InheritedWidget: It is a base class for widgets that efficiently propagates information down the tree. To obtain the near instance of a particular type of inherited widget from a build context, use BuildContext.dependOnInheritedWidgetOfExactType.
  • When referenced in this way, it causes the consumer to rebuild when the inherited widget itself changes state. 
  • ParentDataWidget:  It is a base class for a widget that hooks ParentData information to children of RenderObjectWidgets.  It is used to provide a per-child configuration for RenderObjectWidgets  with more than one child.  
  • LeafRenderObjectWidget: It is a superclass for RenderObjectWidgets that configure RenderObject subclasses that have no children.
  • SingleChildRenderObjectWidget: It is a superclass for RenderObjectWidgets that configure RenderObject subclasses that have a single child slot.
  • MultiChildRenderObjetWidget: It is a superclass for RenderObjectWidgets that configure RenderObject subclasses that have a single list of children.
  • _NullWidget: Zero cost widget. Use it when you need a placeholder.

Most commonly used Flutter Widgets with the example 

1. Appbar

A Material Design app bar. An app bar consists of a toolbar and potentially other widgets, such as a TabBar and a FlexibleSpaceBar.

Example:
import 'package:flutter/material.dart';
void main() {
  runApp(gfgApp()); //MaterialApp
}

MaterialApp gfgApp() {
  return MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text('My demo app'),
      ), //AppBar
      body: const Center(
        child: Text(
          'Hello Geeks',
          style: TextStyle(fontSize: 24),
        ), //Text
      ), // center
    ), //Scaffold
    debugShowCheckedModeBanner: false, //Removing Debug Banner
  );
}

Output:
demo1

2. Column

Layout a list of child widgets in the vertical direction.

Example:
import 'package:flutter/material.dart';


//function to trigger build
void main() {
  runApp(const MyApp());
}


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Demo App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ), // ThemeData
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    ); // MaterialApp
  }
}


class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);


  @override
// ignore: library_private_types_in_public_api
  _MyHomePageState createState() => _MyHomePageState();
}


class _MyHomePageState extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Hello Geeks"),
      ), // AppBar
      // App body consists of single Column
      // Column consists of three children widgets
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.blueAccent),
            child: const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "DhiWise",
                style: TextStyle(color: Colors.white, fontSize: 25),
              ),
            ),
          ),
          Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.lightGreen),
            child: const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "DhiWise",
                style: TextStyle(color: Colors.white, fontSize: 25),
              ),
            ),
          ),
          Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.pinkAccent),
            child: const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "DhiWise",
                style: TextStyle(color: Colors.white, fontSize: 25),
              ),
            ),
          )
        ],
      ), // Column
    );
  }
}
Output:
demo2

3. Container

 A convenience widget that combines common painting, positioning, and sizing widgets.

Example:
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
  return MaterialApp(
  home: Scaffold(
    appBar: AppBar(
    title: const Text("Container example"),
    ),
    body: Container(
    child:const Text("Hello! i am inside a container!",
      style: TextStyle(fontSize: 20)),
    ),
  ),
  );
}
}

Output: 
demo3

4. ElevatedButton

A Material Design elevated button. A filled button whose material elevates when pressed.

Example: 
ElevatedButton(
          child: Text('Elevated Button'),
          style: ElevatedButton.styleFrom(
            primary: Colors.blue,
          ),
          onPressed: () {},
        ),

5. FlutterLogo

The Flutter logo, in the widget form. This widget respects the IconTheme.

Example:
import 'package:flutter/material.dart';
 
//Material design library
void main() {
runApp(
  //widget tree starts here
  MaterialApp(
  home: Scaffold(
    appBar: AppBar(
    leading: Container(
      color: Colors.white,
      padding: EdgeInsets.all(3),
      /** FlutterLogo Widget **/
      child: FlutterLogo(
      size: 10,
      ), //FlutterLogo
    ), //Container
    title: Text('Hello Dev'),
    backgroundColor: Colors.blueAccent[400],
    centerTitle: true,
    ), //AppBar
    body: Center(
    child: Container(
      /** FlutterLogo Widget **/
      child: FlutterLogo(
      size: 300,
      textColor: Colors.blue,
      style: FlutterLogoStyle.stacked,
      ), //FlutterLogo
    ), //Container
    ), //Center
  ), //Scaffold
  ), //MaterialApp
);
}

6. Icon

A Material Design icon.

Example:
import 'package:flutter/material.dart';
 
void main() => runApp(const MyApp());
 
/// main application widget
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
 
  static const String _title = 'Flutter Application';
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}
 
/// stateless widget that the main application instantiates
class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: const [
        Icon(
          Icons.audiotrack,
          size: 100.0,
          color: Colors.green,
        ),
      ],
    );
  }
}
Output:
flutter-app

7. Image

 A widget that displays an image.

Example:
import 'package:flutter/material.dart';  
 
void main() => runApp(MyApp());  
 
class MyApp extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: Scaffold(  
        appBar: AppBar(  
            title: Text('Flutter Image Demo'),  
        ),  
        body: Center(  
          child: Column(  
            children: [  
              Image.asset('assets/Mountains.png'),  //Image source
              Text(  
                  'Eastern valley of lush green mountains.',  // Image description
                  style: TextStyle(fontSize: 20.0),  
              )  
            ],  
          ),  
        ),  
      ),  
    );  
  }  
}   

8. Placeholder

A widget that draws a box that represents where other widgets will one day be added.

Example:
 SizedBox(height: 10),
            Container(
              height: 100,
              child: Placeholder(color: RED),
            ),
 Output:
placeholder

9. Row

 Layout a list of child widgets in the horizontal direction.

Example:
import 'package:flutter/material.dart';


//function to trigger build
void main() {
  runApp(const MyApp());
}


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Demo App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ), // ThemeData
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    ); // MaterialApp
  }
}


class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);


  @override
// ignore: library_private_types_in_public_api
  _MyHomePageState createState() => _MyHomePageState();
}


class _MyHomePageState extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Hello Geeks"),
      ), // AppBar
      // App body consists of single Row
      // Row consists of three children widgets
      body: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.blue),
            child: const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "Welcome",
                style: TextStyle(color: Colors.white, fontSize: 25),
              ),
            ),
          ),
          Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.blue),
            child: const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "to",
                style: TextStyle(color: Colors.white, fontSize: 25),
              ),
            ),
          ),
          Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.blue),
            child: const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "DhiWise",
                style: TextStyle(color: Colors.white, fontSize: 25),
              ),
            ),
          )
        ],
      ),
    );
  }
}
Output:
row-img

10. Text

A run of text with a single style.

Example:

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
 
void main() {
  runApp(MyApp());
}
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter Text Widget"),
        ),
        body: Text("This is text"),
      ),
    );
  }
}

Output:

flutter.text.widget

Also, read our article on medium to know about the popular animation widgets for creating an intuitive UI. 

Summing up: 

And here you have it; everything about the Flutter widgets!! Hope you find it useful to get a quick view of Flutter's basic widgets, categories, and types. 

Well, if you are looking for a way to make your app development faster without affecting code quality try DhiWise- A ProCode app development platform for web and mobile application development. 

Its Flutter app builder provides you with the state of the art features, such as Figma to flutter, state management, flexibility to customization, team collaboration, complete code ownership, and more. 

So, don’t waste your precious time on repetitive tasks in app development, sign up now to generate your production code in just a few steps.