Skip to main content

Flutter AppBar with an Easy Example

 


In Flutter, the AppBar is a simple yet powerful widget that provides the top app bar typically found in mobile applications. It is a critical part of your app's layout and design, giving users easy access to navigation, search, actions, and other features in your app.

In this blog post, we will explore the AppBar widget in Flutter, how to customize it, and provide a simple example to help you get started.

------------------------------------------------------------------------

What is an AppBar in Flutter?

The AppBar widget in Flutter is a material design app bar that can be used to display titles, icons, action buttons, and other important elements at the top of your app screen.

The AppBar is usually placed inside a Scaffold widget, which provides a basic layout structure for the app. 

Key Features of AppBar:

  1. Title: Displays the main title of the screen.

  2. Actions: Typically used to place action icons like search or menu buttons.

  3. Leading: Commonly used for the back button or a drawer button (hamburger menu).

  4. FlexibleSpace: Customizes the area in the app bar where you can add complex widgets.

  5. Bottom: Option to display a bottom widget (such as a tab bar).


Basic Syntax of AppBar:

AppBar(
title: Text("My App Title"),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
// Handle search action }, ), ], )

Customizing the AppBar

Flutter's AppBar offers many options to customize its appearance and functionality. Here are some commonly used properties:

  1. title: The main text displayed on the app bar.

  2. leading: A widget displayed at the beginning of the app bar. It’s often used for a back button or a hamburger menu.

  3. actions: A list of widgets to display at the end of the app bar. It is often used to show action buttons like settings, search, or user profile.

  4. backgroundColor: Sets the color of the app bar.

  5. elevation: The shadow depth of the app bar, which can make it appear as though it is floating.


Example of a Simple AppBar:

Below is an easy-to-understand example of a Flutter app with a customized AppBar.


import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomeScreen(), ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("My Flutter App"), leading: IconButton( icon: Icon(Icons.menu), onPressed: () { // Handle menu button press print("Menu button pressed"); }, ), actions: [ IconButton( icon: Icon(Icons.search), onPressed: () { // Handle search button press print("Search button pressed"); }, ), IconButton( icon: Icon(Icons.settings), onPressed: () { // Handle settings button press print("Settings button pressed"); }, ), ], backgroundColor: Colors.blue, elevation: 4.0, ), body: Center( child: Text('Welcome to the Home Screen!'), ), ); } }

Explanation of the Code:

  1. MaterialApp: The app starts with a MaterialApp widget, which sets up the basic structure of a Material Design app.

  2. Scaffold: The Scaffold widget provides the basic layout structure, including the AppBar and body.

  3. AppBar:

    • title: Displays "My Flutter App".

    • leading: An IconButton with a hamburger menu icon.

    • actions: A list of buttons, including a search and settings icon.

    • backgroundColor: Sets the background color to blue.

    • elevation: Adds a shadow to the app bar to give it a floating effect.


How to Test This Code:

  1. Set Up Flutter: Ensure you have Flutter set up on your computer. If not, follow the instructions on the Flutter website.

  2. Create a New Project: Run flutter create appbar_example to create a new Flutter project.

  3. Replace the main.dart: Replace the content of lib/main.dart with the above code.

  4. Run the App: Use flutter run to run the app on an emulator or device.


Conclusion

The AppBar in Flutter is an essential component that helps structure the top part of your app and provides an easy way to add common features like navigation and actions. In this blog post, we learned about the AppBar, its properties, and how to customize it for your app.

Now, you can explore more complex use cases, such as adding tabs, flexible space, or integrating a custom search bar into the app bar. Happy coding, and I hope you find Flutter even more exciting as you dive deeper into app development!

Comments

Your Complete Flutter Roadmap: From Beginner to Pro

🌀 Flutter App Lifecycle – Easy Way to Understand

  When you build an app in Flutter , it goes through different states depending on how the user interacts with it (open, minimize, back button, etc). Think of it like a human day cycle – Wake up → Active → Sleep → Wake again . 🌱 1. main() – Birth of the App Every Flutter app starts from the main() function. void main() { runApp(MyApp()); } 👉 This is like turning ON the lights – your app is born. 🏗 2. Widgets Lifecycle In Flutter, everything is a Widget . There are two main types: StatelessWidget → UI that doesn’t change. StatefulWidget → UI that changes with user interaction. For StatefulWidget , the lifecycle looks like this: 🔹 When Widget is created createState() → Creates a State object (like preparing memory). initState() → First time setup (like waking up, stretching, preparing breakfast). Best place for API calls , animation controllers , initial values . 🔹 When Widget builds build() → Renders UI (like dressin...

📱 Flutter ListTile and ListView: Explained with Easy Examples

Flutter ListTile and ListView: When you start learning Flutter, two widgets often confuse beginners: ListTile  &  ListView . Both are used to display items in a list, but their purpose is different. Let’s break them down in a simple way. 🔹 What is ListTile in Flutter? ListTile is a ready-made widget used to show a single row (one item). It usually contains: title (main text) subtitle (small text under the title) leading (icon/image on the left) trailing (icon/text on the right) 👉 Think of ListTile as a single row card inside a list. ✅ Example of ListTile: import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text("Flutter ListTile Example")), body: const ListTile( leading:...

Text Widget In Flutter

  The Text widget in Flutter is used to display text on the screen. It's one of the most fundamental widgets and supports a wide range of customizations to style, align, and manage text. Here’s a detailed overview: Basic Syntax dart Copy code Text( 'Hello, Flutter!', style: TextStyle(fontSize: 24, color: Colors.blue), textAlign: TextAlign.center, ) Key Properties data : The text string to display. Example: 'Hello, World!' . style : Defines the appearance of the text. Uses the TextStyle class for customization. Example : dart Copy code Text( 'Styled Text', style: TextStyle( fontSize: 20, fontWeight: FontWeight.bold, color: Colors.red, fontStyle: FontStyle.italic, letterSpacing: 2.0, wordSpacing: 5.0, decoration: TextDecoration.underline, ), ) textAlign : Aligns the text horizontally. Values: TextAlign.left , TextAlign.right , TextAlign.center , TextAlign.justify , etc. Example: dart Copy code textAlign: TextAlign.cente...