abuanwar072 / Flutter-Responsive-Admin-Panel-or-Dashboard

Responsive Admin Panel or Dashboard using Flutter
https://youtu.be/_uOgXpEHNbc
MIT License
6.61k stars 1.87k forks source link

named route #67

Closed yusufelyldrm closed 12 months ago

yusufelyldrm commented 1 year ago

How can ı add named route to pages(for example instagram website has side menu too but when you go to messages url goes to instagram.com/direct/messages ı want to do it like that in my project. I looked around internet but couldn't find it. )

Dantechdevs commented 10 months ago

To create named routes with Flutter for navigation similar to Instagram's side menu behavior, you can use the Flutter package called "flutter_bloc" in combination with "flutter_bloc" or "Provider" for state management. Here's a general outline of how to do it:

  1. Add Dependencies: In your pubspec.yaml file, make sure to include the necessary packages.
dependencies:
  flutter:
    sdk: flutter
  flutter_bloc: ^7.0.0
  # Other dependencies as needed
  1. Create Named Routes: Define named routes in your app's MaterialApp by using the routes property:
MaterialApp(
  routes: {
    '/home': (context) => HomeScreen(),
    '/profile': (context) => ProfileScreen(),
    '/messages': (context) => MessagesScreen(),
    // Add other routes as needed
  },
  // Your other MaterialApp configuration
)
  1. Implement Navigation: To navigate between these routes and mimic Instagram's side menu behavior, you can use a package like flutter_bloc or Provider for state management.

    • Create a Bloc (if using flutter_bloc) or a Provider to manage the current route state.
    • Update the state when the user selects a different menu item.

For example, if using flutter_bloc, you could create a simple Bloc to manage the selected route and use a BlocBuilder to rebuild the UI when the route changes:

class RouteBloc extends Bloc<String, String> {
  RouteBloc() : super('/home');

  @override
  Stream<String> mapEventToState(String event) async* {
    yield event;
  }
}

// In your app's main function:
void main() {
  runApp(
    MaterialApp(
      home: BlocProvider(
        create: (context) => RouteBloc(),
        child: App(),
      ),
    ),
  );
}

// In your UI, use BlocBuilder to conditionally display the selected route:
BlocBuilder<RouteBloc, String>(
  builder: (context, route) {
    switch (route) {
      case '/home':
        return HomeScreen();
      case '/profile':
        return ProfileScreen();
      case '/messages':
        return MessagesScreen();
      // Add cases for other routes
      default:
        return HomeScreen(); // Default route
    }
  },
)
  1. Trigger Navigation: To navigate to different routes, you can use the Navigator or use widgets like InkWell or GestureDetector for handling tap events.

For example, to navigate to the "Messages" route:

GestureDetector(
  onTap: () {
    context.read<RouteBloc>().add('/messages'); // Use context.read with Provider
  },
  child: Text('Messages'),
)

By following this approach, you can set up named routes and dynamic navigation in your Flutter app, similar to Instagram's side menu behavior. Make sure to adapt it to your specific project structure and state management preferences.

yusufelyldrm commented 10 months ago

Thank you for this explanation 😊