I am trying to create simple app with next layout:
Buttons should switch Widget1\Widget2.
I wrote next code (copy-past ready):
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: HomeBinding(),
initialRoute: '/widget1',
home: Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: SafeArea(
child: IndexedStack(
index: Get.find<NavigationBarController>().tabIndex.value,
children: [
Widget1(),
Widget2()
],
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: Get.find<NavigationBarController>().tabIndex.value,
onTap: Get.find<NavigationBarController>().changeTabIndex,
items: [
_bottomNavigationBarItem(
icon: CupertinoIcons.home,
label: 'Button1',
),
_bottomNavigationBarItem(
icon: CupertinoIcons.rocket_fill,
label: 'Button2',
),
])),
getPages: [
GetPage(
name: '/widget1',
page: () => Widget1(),
binding: Widget1_Bindings(),
),
GetPage(
name: '/widget2',
page: () => Widget2(),
binding: Widget2_Bindings(),
),
],
debugShowCheckedModeBanner: false,
themeMode: ThemeMode.system,
);
}
}
class NavigationBarController extends GetxController {
static NavigationBarController get to => Get.find();
var tabIndex = 0.obs;
void changeTabIndex(int index) {
tabIndex.value = index;
update();
}
@override
void onInit() {
super.onInit();
}
@override
void dispose() {
super.dispose();
}
}
class Controller1 extends GetxController {
var _test2 = "test1".obs;
get test1 => this._test2.value;
}
class Controller2 extends GetxController {
var _test2 = "test2".obs;
get test1 => this._test2.value;
}
class Widget1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text("Widget1"),
);
}
}
class HomeBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => NavigationBarController());
}
}
class Widget1_Bindings extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => Controller1());
}
}
class Widget2_Bindings extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => Controller2());
}
}
class Widget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text("Widget2"),
);
}
}
_bottomNavigationBarItem({required IconData icon, required String label}) {
return BottomNavigationBarItem(
icon: Icon(icon),
label: label,
);
}
Code have two problems. First one is that I am getting error:
The second one is that I can;t figure out how to get GetX routing like: Get.to
work.