Flutter – CupertinoNavigationBar Widget
Last Updated :
16 Oct, 2023
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:
middle:
trailing:
backgroundColor:
border:
previousPageTitle:
automaticallyImplyLeading:
actionsForegroundColor:
transitionBetweenRoutes:
heroTag:
borderColor:
borderOpacity:
)
|
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' ),
leading: CupertinoButton(
child: Text(
'Back' ,
style: TextStyle(fontSize: 13),
),
onPressed: () {
Navigator.of(context).pop();
},
),
trailing: CupertinoButton(
child: Text(
'Save' ,
style: TextStyle(fontSize: 13),
),
onPressed: () {
},
),
),
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' ),
leading: CupertinoButton(
child: Text(
'Back' ,
style: TextStyle(fontSize: 13),
),
onPressed: () {
Navigator.of(context).pop();
},
),
trailing: CupertinoButton(
child: Text(
'Save' ,
style: TextStyle(fontSize: 13),
),
onPressed: () {
},
),
),
child: Center(
child: Text( 'Your content goes here.' ),
),
);
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...