Hello World...

Flutter current path 본문

flutter

Flutter current path

FaustK 2023. 12. 6. 16:16

current path 를 확인하려면 ModalRoute 를 사용하면 된다.

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    // current path 를 알기 위해 활용
    var route = ModalRoute.of(context);

    if (route != null) {
      print('current path: ${route.settings.name}');
    }
    // -------------------------------

    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          DrawerHeader(
            child: Column(
              children: [
                Text(
                  'Best Shop',
                  style: TextStyle(
                    color: Theme.of(context).colorScheme.inversePrimary,
                    fontSize: 24,
                  ),
                ),
              ],
            ),
          ),
          ListTile(
            leading: const Icon(Icons.shopping_bag),
            title: const Text('Shop'),
            onTap: () {
              Navigator.pop(context);

              if (route?.settings.name != '/shop_screen') {
                Navigator.pop(context);
                Navigator.pushNamed(context, '/shop_screen');
              }
            },
          ),

          ListTile(
            leading: const Icon(Icons.shopping_cart),
            title: const Text('Cart'),
            onTap: () {
              Navigator.pop(context);

              if (route?.settings.name != '/cart_screen') {
                Navigator.pop(context);
                Navigator.pushNamed(context, '/cart_screen');
              }
            },
          )
        ],
      ),
    );
  }
}

 

Comments