import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/pages/dashboard/controller.dart';
import 'package:vnoteapp/pages/dashboard/view.dart';
import 'package:vnoteapp/routes/routes.dart';

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

  @override
  Widget build(BuildContext context) {
    return Navigator(
      key: Get.nestedKey(
        1001,
      ),
      initialRoute: "/dashboard",
      onGenerateRoute: onGenerateRoute,
    );
  }

  /// 路由生成器
  Route? onGenerateRoute(RouteSettings settings) {
    String? name = settings.name;
    if (name == null) return null;

    name = name.split("?")[0];
    if (name == "/") return null;

    final route = Routes.routes.firstWhereOrNull((e) => e.name == name);
    if (route != null) {
      return GetPageRoute(
        page: route.page,
        binding: route.binding,
        transition: Transition.noTransition,
      );
    } else {
      return GetPageRoute(
        page: () => Container(
          color: Colors.white,
          child: Center(
            child: Text(
              "Route: $name",
              style: const TextStyle(fontSize: 32),
            ),
          ),
        ),
        binding: BindingsBuilder(() {}),
        transition: Transition.noTransition,
      );
    }
  }
}
