I was trying to implement the navigation between screens. I have the first screen, where the user must insert the invitation code. At this point user is not authorised yet, and I don't want him/her to use the bottom navigation bar. Here is my implementation of the navigation:
home_view.dart
import 'package:fauna/app/styles/colors.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'home_view_model.dart';
class HomeView extends GetView<HomeController> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Navigator(
key: Get.nestedKey(1),
initialRoute: '/invite-code',
onGenerateRoute: controller.onGenerateRoute,
),
bottomNavigationBar: Obx(() => BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
FontAwesomeIcons.baby,
color: vaccineValid,
size: 15,
),
title: Text('Pet details'),
),
BottomNavigationBarItem(
icon: Icon(
FontAwesomeIcons.airbnb,
color: vaccineValid,
size: 15,
),
title: Text('Vet profile'),
),
],
currentIndex: controller.currentIndex.value,
selectedItemColor: Colors.green,
onTap: controller.changePage,
)),
);
}
}
home_binding.dart
import 'package:fauna/app/modules/home/home_view_model.dart';
import 'package:get/get.dart';
class HomeBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => HomeController());
}
}
home_controller.dart
import 'package:fauna/app/modules/invite_code/invite_code_binding.dart';
import 'package:fauna/app/modules/invite_code/invite_code_view.dart';
import 'package:fauna/app/modules/pet_details/pet_details_binding.dart';
import 'package:fauna/app/modules/pet_details/pet_details_view.dart';
import 'package:fauna/app/modules/vet_profile/vet_profile_binding.dart';
import 'package:fauna/app/modules/vet_profile/vet_profile_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomeController extends GetxController {
late BuildContext context;
static HomeController get to => Get.find();
var currentIndex = 0.obs;
final pages = <String>['/pet-details', '/vet-profile'];
var count = 0.obs;
void changePage(int index) {
currentIndex.value = index;
Get.toNamed(pages[index], id: 1);
}
Route? onGenerateRoute(RouteSettings settings) {
if (settings.name == '/invite-code') {
return GetPageRoute(
settings: settings,
page: () => InviteCodeView(),
binding: InviteCodeBinding(),
);
}
if (settings.name == '/pet-details') {
return GetPageRoute(
settings: settings,
page: () => PetDetailsView(),
binding: PetDetailsBinding(),
);
}
if (settings.name == '/vet-profile') {
return GetPageRoute(
settings: settings,
page: () => VetProfileView(),
binding: VetProfileBinding(),
);
}
return null;
}
}