DEV Community

Miguel Ramirez
Miguel Ramirez

Posted on

flutter #2 #routes - simple

De todas las Page creadas en directorio /screens creamos un exportador de Pages Index.screens.dart.
Image description

export 'package:myapp/screens/Home.screen.dart';
export 'package:myapp/screens/Register.screen.dart';
export 'package:myapp/screens/Dashboard.screen.dart';
Enter fullscreen mode Exit fullscreen mode

Creamos Routes.dart donde importamos el exportador de Pages y se Mapea para entregarse con su ruta en la funcion

getRoutes()

import 'package:myapp/screens/Index.screens.dart';

  Map<String, WidgetBuilder> getRoutes() {
    Map<String, WidgetBuilder> routes = Map();
    routes['/'] = (_) => HomePage();
    routes['/register'] = (_) => RegisterPage();
    routes['/dashboard'] = (_) => DashboardPage();
    return routes;
  }
Enter fullscreen mode Exit fullscreen mode

En main.dart importamos el Routes.dart trayendo la funcion

En MaterialApp agregamos:

  • initialRoute: '/'
  • routes: getRoutes() // con el mapeo de todas las rutas.
import 'package:flutter/material.dart';
import 'package:myapp/routers/Routes.dart'; // enrutador

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      debugShowCheckedModeBanner: false,
      initialRoute: '/',
      routes: getRoutes(),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

LISTO!

Top comments (0)