Intermediate
Explain the concept of routing in Flutter.
Routing in Flutter refers to the process of navigating between different screens or pages within an app. Flutter provides a robust routing system to manage navigation effectively.
Here’s a step-by-step explanation of how routing works:
- Define Routes: You can define routes in the MaterialApp widget using the 'routes' property.
- Navigate: Use Navigator.push() to navigate to a new route and Navigator.pop() to return to the previous one.
- Named Routes: Use named routes for easier management of navigation.
Example code snippet:
void main() {
runApp(MaterialApp(
routes: {
'/': (context) => HomeScreen(),
'/details': (context) => DetailsScreen(),
},
));
}
// Navigate to details screen:
Navigator.pushNamed(context, '/details');