ข้ามไปยังเนื้อหา

Named Routes (เส้นทางที่ตั้งชื่อ)

ใน React Router คุณกำหนด route configuration — array หรือ JSX tree ของ <Route path="..." element={...} /> objects — และ router จะ render component ที่ตรงกันตาม URL ปัจจุบัน Flutter มีสิ่งที่เทียบเคียงได้แบบ built-in ที่เรียบง่ายกว่า: routes map บน MaterialApp ที่เชื่อมโยง string name กับ builder function การเรียก Navigator.pushNamed(context, '/detail') จะค้นหาชื่อใน map และ push หน้าจอที่ตรงกัน

Named routes แก้ปัญหาเดียวกับ path config ของ React Router: คุณแยก navigation call site ออกจาก screen widget จริง ๆ ทำให้สามารถเปลี่ยนชื่อหรือย้าย screen ได้โดยไม่ต้องอัปเดตทุก push call

React
// React Router v6 — route config
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
const router = createBrowserRouter([
{ path: '/', element: <HomeScreen /> },
{ path: '/detail', element: <DetailScreen /> },
{ path: '/profile', element: <ProfileScreen /> },
]);
// Navigate by path string
navigate('/detail');
navigate('/profile');
Flutter
// Flutter — named routes map
MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => const HomeScreen(),
'/detail': (context) => const DetailScreen(),
'/profile': (context) => const ProfileScreen(),
},
);
// Navigate by name
Navigator.pushNamed(context, '/detail');
Navigator.pushNamed(context, '/profile');
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/':        (context) => const HomeScreen(),
        '/about':   (context) => const AboutScreen(),
        '/contact': (context) => const ContactScreen(),
      },
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ElevatedButton(
              onPressed: () => Navigator.pushNamed(context, '/about'),
              child: const Text('About'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => Navigator.pushNamed(context, '/contact'),
              child: const Text('Contact'),
            ),
          ],
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('About')),
      body: const Center(child: Text('About page')),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Contact')),
      body: const Center(child: Text('Contact page')),
    );
  }
}
คุณกำหนด named routes map ไว้ที่ไหนในแอป Flutter?
อะไรคือสิ่งที่เทียบเท่ากับ navigate(path, { replace: true }) ของ React Router ใน Flutter?
ข้อจำกัดสำคัญของ Flutter built-in named routes เทียบกับ React Router คืออะไร?
initialRoute ทำอะไรใน Flutter MaterialApp?