Open In App

Flutter – CupertinoNavigationBar Widget

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




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:



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.




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.




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.




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




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:


Article Tags :