Responsive Ad Code Here

Header Ads

go router flutter

 install the despondency 

 
    go_router: ^14.1.4


create router.dart page 


import 'package:flutter_package_plus/go_router/go_router.dart';
import 'package:go_router/go_router.dart'; // package

class AppRouter {
  GoRouter goRouter = GoRouter(
    initialLocation: '/home', // intre page
    routes: [
      GoRoute(
        path: '/home', // path and this path use all location
        name: '/home', // path name the name show url
        builder: (context, state) {
          return const GoRouterFlutter(); // path location
        },
      ),
      GoRoute(
        path: '/go', // path and this path use all location
        name: '/go', // path name the name show url
        builder: (context, state) {
          return const GoPage(); // path location
        },
      ),

     
    ],
  );
}


create any page 



import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class GoRouterFlutter extends StatefulWidget {
  const GoRouterFlutter({super.key});

  @override
  State<GoRouterFlutter> createState() => _GoRouterFlutterState();
}

class _GoRouterFlutterState extends State<GoRouterFlutter> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
        context.goNamed('/go');//goName is come to main package and it's main work navigate
          }, // and go is path
          child: const Text('GoPage'),
        ),
      ),
    );
  }
}

class GoPage extends StatefulWidget {
  const GoPage({super.key});

  @override
  State<GoPage> createState() => _GoPageState();
}

class _GoPageState extends State<GoPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          height: 200,
          width: 200,
          color: Colors.orange,
        ),
      ),
    );
  }
}


require other wise the package not work 

go main.dart 

import 'package:flutter/material.dart';
import 'package:flutter_package_plus/go_router/router.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router( // Material convert the router didn't come to the use package
      title: 'Flutter Package',
      theme: ThemeData(
        useMaterial3: true,
      ),
      routerConfig: AppRouter().goRouter, // this is important
    );
  }
}