Flutter Tutorial for Beginners #1 - AppBar Widget

In this tutorial we will take a look at how to use Flutter AppBar Widget and Implement AppBar widget with a simple Flutter Application.

Flutter Tutorial for Beginners #1 - AppBar Widget
Flutter Tutorial for Beginners for AppBar - mobilelabs.in

What you will learn?

  1. What is Flutter Widget
  2. Stateful Widget vs Statless Widget
  3. How to use AppBar Widget

We need to understand what is Flutter Widget

Every pices in the Flutter is build with Widget, Flutter Application is built up with the Widget Tree.

Widget Types

  1. Stateful Widget
  2. Stateless Widget

Stateful Widget

Flutter Stateless - mobilelabs.in

These type of widget never changes, these widget are created by extending the StatelessWidget class.

Stateful Widget

Flutter Stateless - mobilelabs.in

These type widgets maintains a State object separating the the appearance state from the widget. When ever State object is changed (setState() is used) it will rebuild the widget.

AppBar Widget is a stateful widget, which placed at the top of the screen. It is used to show leading, title, actions and bottom.

Thank you - https://api.flutter.dev

Let's Code

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Mobilelabs'),
        ),
      ),
    );
  }
}

Now we have out AppBar widget with the title(Text widget).

Flutter AppBar - mobilelabs.in

Let's make some change by adding leading widget and color.

  leading: Icon(
    Icons.label_important,
    color: Colors.white,
    size: 30.0,
  ),
  centerTitle: false,

Final code

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.amber,
      ),
      home: Scaffold(
        appBar: AppBar(
          leading: Icon(
            Icons.label_important,
            color: Colors.white,
            size: 30.0,
          ),
          centerTitle: false,
          title: Text('Mobilelabs'),
        ),
      ),
    );
  }
}

I have added icon as a leading widget in above code and change the title position to left aligned.

Also changed the primary theme color to amber.

Output

Flutter AppBar - mobilelabs.in

Conclusion

In this we have seen basic of flutter widget and stateful Vs stateless widgets also we have seen how to use flutter AppBar with the real world example.

Thank you for reading, please do share if you like it.

How to use SelectableText
Do you want to build a selectable text in your flutter application? Try SelectableText widget. They give better user experience to the user and make them to feel like a native application.
How to use AlertDialog?
Does you application needs to alert your user or maybe to get input from them? Try using Flutter AlertDialog widget.
How to build stunning UI/UX with ColorFiltered widget
Sometimes its not easy to modify the color of the widget easily but using the ColorFiltered widget it helps to build out the better user experience