Routes With Getx in Flutter
Hi Coders,
Hope you doing great, and if you still didn't watch the video on this Topic then go and watch and also read our previous part 1 article on Getx Integration of Flutter Getx Series
Main.dart
main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
initialRoute: AppPages.initial,
getPages: AppPages.routes,
);
}
}
app_pages.dart
abstract class AppPages {
AppPages._();
static const initial = Routes.home;
static final routes = [
/// Home page
GetPage(
name: Routes.home,
page: () => const HomepageView(),
binding: HomepageBinding()),
// Testing page
GetPage(
name: Routes.testing,
page: () => const TestingView(),
binding: TestingBinding()),
];
}
app_routes.dart
abstract class Routes {
static const home = _Paths.home;
static const testing = _Paths.testing;
}
abstract class _Paths {
static const home = '/home';
static const testing = '/testing';
}
homepage_view.dart
class HomepageView extends StatelessWidget {
const HomepageView({super.key});
@override
Widget build(BuildContext context) {
HomepageController homepageController = Get.put(HomepageController());
return Scaffold(
body: Obx(
() => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
homepageController.counter.value.toString(),
style: const TextStyle(fontSize: 45.0),
),
ElevatedButton(
onPressed: () {
Get.toNamed(Routes.testing);
},
child: const Text("Go to Testing Screen"))
],
),
),
),
floatingActionButton: ElevatedButton(
onPressed: () {
homepageController.updateCounterValue();
},
child: const Text("Add")),
);
}
}
testing_view.dart
import 'package:flutter/material.dart';
class TestingView extends StatelessWidget {
const TestingView({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("This is Testing screen"),
),
);
}
}
testing_controller.dart
import 'package:get/get.dart';
class TestingController extends GetxController {}
testing_binding.dart
import 'package:codemicros_getx_series/app/views/testing/testing_controller.dart';
import 'package:get/get.dart';
class TestingBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => TestingController());
}
}
------- THANK YOU -------