Open In App

Flutter – CupertinoNavigationBar Widget

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The CupertinoNavigationBar widget in Flutter is a part of the Cupertino package, which is designed to provide an iOS-style look and feel to your Flutter app. This widget represents the navigation bar that is typically found at the top of an iOS app’s screen. In this article, we are going to implement the CupertinoNavigationBar widget and explore some properties of it.

Basic Syntax of CupertinoNavigationBar Widget

Dart




CupertinoNavigationBar(
  leading: // Widget for the leading position,
  middle: // Widget for the middle position (typically a title),
  trailing: // Widget for the trailing position,
  backgroundColor: // Background color of the navigation bar,
  border: // Border configuration for the navigation bar,
  previousPageTitle: // Title of the previous page for back gestures,
  automaticallyImplyLeading: // true or false,
  actionsForegroundColor: // Color for action buttons and back button,
  transitionBetweenRoutes: // true or false,
  heroTag: // Hero tag for hero transitions,
  borderColor: // Color of the navigation bar's border,
  borderOpacity: // Opacity of the navigation bar's border,
)


Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementations

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

First of all import material.dart file.

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

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




void main() {
  runApp(MyApp());
}


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      title: 'CupertinoNavigationBar Example',
      home: CupertinoNavigationBarExample(),
    );
  }
}


Step 5: Create CupertinoNavigationBarExample Class

In this class we are going to Implement the CupertinoNavigationBar widget that help to display a IOS styled navigation window. Comments are added for better understanding.

Dart




class CupertinoNavigationBarExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Cupertino Navigation Bar'), // Title in the middle
        leading: CupertinoButton(
          child: Text(
            'Back',
            style: TextStyle(fontSize: 13),
          ),
          onPressed: () {
            // Add your back button action here
            Navigator.of(context).pop();
          },
        ),
        trailing: CupertinoButton(
          child: Text(
            'Save',
            style: TextStyle(fontSize: 13),
          ),
          onPressed: () {
            // Add your save button action here
          },
        ),
      ),
      child: Center(
        child: Text('Your content goes here.'),
      ),
    );
  }
}


Here is the full Code of main.dart file

Dart




import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      title: 'CupertinoNavigationBar Example',
      home: CupertinoNavigationBarExample(),
    );
  }
}
  
class CupertinoNavigationBarExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Cupertino Navigation Bar'), // Title in the middle
        leading: CupertinoButton(
          child: Text(
            'Back',
            style: TextStyle(fontSize: 13),
          ),
          onPressed: () {
            // Add your back button action here
            Navigator.of(context).pop();
          },
        ),
        trailing: CupertinoButton(
          child: Text(
            'Save',
            style: TextStyle(fontSize: 13),
          ),
          onPressed: () {
            // Add your save button action here
          },
        ),
      ),
      child: Center(
        child: Text('Your content goes here.'),
      ),
    );
  }
}


Output:

Screenshot_2023-10-02-20-32-14-381_comexampledismissable-(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads