7TH_MOB_COMPUTING-QUESTION ANSWER
7TH_MOB_COMPUTING-QUESTION ANSWER
The routes.dart file in a Flutter application is used to define and manage routes in a
structured way. It centralizes the navigation logic, making it easier to maintain and
scale the app.
import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'profile_screen.dart';
Page1of6
MOBILE COMPUTING QUESTION ANSWER
class AppRoutes {
static const String home = '/';
static const String profile = '/profile';
Usage in main.dart :
void main() {
runApp(MaterialApp(
initialRoute: AppRoutes.home,
routes: AppRoutes.getRoutes(),
));
}
The Navigator in Flutter is responsible for managing the app’s stack of pages (routes).
It allows users to navigate between screens using push and pop operations.
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NewScreen()),
);
To go back:
dart
CopyEdit
Navigator.pop(context);
Page2of6
MOBILE COMPUTING QUESTION ANSWER
Navigator.pushNamed(context, '/profile');
Navigator.pop(context); // Go back
Navigator.pushReplacementNamed(context, '/home'); // Replace current screen
void main() {
runApp(MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/profile': (context) => ProfileScreen(),
},
));
}
Usage
Navigator.pushNamed(context, '/profile');
Page3of6
MOBILE COMPUTING QUESTION ANSWER
Example :
Navigator.pushNamed(
context,
'/profile',
arguments: {'username': 'JohnDoe'},
);
Page4of6
MOBILE COMPUTING QUESTION ANSWER
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NestedScreen()),
);
Deep linking allows external URLs to navigate to specific screens in your app.
Example: onGenerateRoute :
onGenerateRoute: (settings) {
if (settings.name == '/profile') {
return MaterialPageRoute(builder: (context) => ProfileScreen());
}
return null;
},
Example: onUnknownRoute :
onUnknownRoute: (settings) {
return MaterialPageRoute(builder: (context) => NotFoundScreen());
},
Page5of6
MOBILE COMPUTING QUESTION ANSWER
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => NewScreen(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(opacity: animation, child: child);
},
),
);
Usage
context.go('/profile');
Page6of6