I have added Obx()
for updating my UI as per with the variables.obs
. It did not work so after that I added variables.refresh()
and update()
for updating the onChanged()
function. Still my UI is not updating.
Home Screen
class HomeScreen extends GetView<HomeController> {
HomeScreen({super.key});
ColorManager colorController = ColorManager();
ImageManager imageController = ImageManager();
// final HomeController homeController = Get.put(HomeController());
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
backgroundColor: colorController.whiteColor,
body: SingleChildScrollView(
padding: EdgeInsets.only(top: 38, bottom: 18, right: 18, left: 18),
child: Column(
spacing: 18,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Obx(() {
return SizedBox(
height: height * 0.05,
child: ListView.separated(
separatorBuilder: (context, index) => SizedBox(width: 8),
scrollDirection: Axis.horizontal,
itemCount: controller.fashionList.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
controller.setCategory(index);
},
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(18),
border: Border.all(
color:
controller.fashionCategoryIndex.value == index
? Colors.transparent
: colorController.primaryColor,
),
color: controller.fashionCategoryIndex.value == index
? colorController.primaryColor
: colorController.whiteColor,
),
child: Padding(
padding: const EdgeInsets.only(left: 18, right: 18),
child: Text(
style: TextStyle(
color:
controller.fashionCategoryIndex.value == index
? colorController.whiteColor
: colorController.primaryColor,
),
textAlign: TextAlign.center,
controller.fashionListTitle[index],
),
),
),
);
},
),
);
}),
Obx(() {
return GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 5 / 7,
crossAxisSpacing: 18,
mainAxisSpacing: 12,
),
physics: NeverScrollableScrollPhysics(),
itemCount: controller
.fashionList[controller.fashionCategoryIndex.value]
.length,
itemBuilder: (context, index) {
return FashionProductCard(
fashionModel:
controller.fashionList[controller
.fashionCategoryIndex
.value][index],
);
},
);
}),
],
),
),
bottomNavigationBar: BottomNavigationBar(
elevation: 28,
backgroundColor: colorController.whiteColor,
selectedItemColor: colorController.primaryColor,
unselectedItemColor: colorController.blackColor,
type: BottomNavigationBarType.fixed,
currentIndex: controller.navBarItemSelectedIndex.value,
onTap: (index) {
controller.setNavBarItem(index);
},
items: [
BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
BottomNavigationBarItem(
label: "Trending",
icon: Icon(Icons.newspaper_outlined),
),
BottomNavigationBarItem(
label: "Favorites",
icon: Icon(Icons.favorite_border),
),
BottomNavigationBarItem(
label: "Cart",
icon: Icon(Icons.shopping_bag_outlined),
),
BottomNavigationBarItem(
label: "Home",
icon: Icon(Icons.person_3_outlined),
),
],
),
);
}
}
HomeScreen Controller
class HomeController extends GetxController {
RxInt fashionCategoryIndex = 0.obs;
RxInt navBarItemSelectedIndex = 0.obs;
setNavBarItem(int index) {
navBarItemSelectedIndex.value = index;
navBarItemSelectedIndex.refresh();
update();
}
setCategory(int index) {
fashionCategoryIndex.value = index;
fashionCategoryIndex.refresh();
update();
}
}
Home Screen
class HomeScreen extends StatelessWidget {
// changed code here
Obx(() {
print(
"This is fashionCategory inside widget: ${homeController.fashionCategoryIndex.value}.",
);
return SizedBox(
height: height * 0.05,
child: ListView.separated(
separatorBuilder: (context, index) => SizedBox(width: 8),
scrollDirection: Axis.horizontal,
itemCount: homeController.fashionList.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
homeController.setCategory(index);
},
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(18),
border: Border.all(
color:
homeController.fashionCategoryIndex.value ==
index
? Colors.transparent
: colorController.primaryColor,
),
color:
homeController.fashionCategoryIndex.value == index
? colorController.primaryColor
: colorController.whiteColor,
),
child: Padding(
padding: const EdgeInsets.only(left: 18, right: 18),
child: Text(
style: TextStyle(
color:
homeController.fashionCategoryIndex.value ==
index
? colorController.whiteColor
: colorController.primaryColor,
),
textAlign: TextAlign.center,
homeController.fashionListTitle[index],
),
),
),
);
},
),
);
}),
Obx(() {
return GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 5 / 7,
crossAxisSpacing: 18,
mainAxisSpacing: 12,
),
physics: NeverScrollableScrollPhysics(),
itemCount: homeController
.fashionList[homeController.fashionCategoryIndex.value]
.length,
itemBuilder: (context, index) {
return FashionProductCard(
fashionModel:
homeController.fashionList[homeController
.fashionCategoryIndex
.value][index],
);
},
);
}),
}
BottomNavigationBar
bottomNavigationBar: Obx(
() => BottomNavigationBar(
elevation: 28,
backgroundColor: colorController.whiteColor,
selectedItemColor: colorController.primaryColor,
unselectedItemColor: colorController.blackColor,
type: BottomNavigationBarType.fixed,
currentIndex: homeController.navBarItemSelectedIndex.value,
onTap: (index) {
homeController.setNavBarItem(index);
},
items: [
BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
BottomNavigationBarItem(
label: "Trending",
icon: Icon(Icons.newspaper_outlined),
),
BottomNavigationBarItem(
label: "Favorites",
icon: Icon(Icons.favorite_border),
),
BottomNavigationBarItem(
label: "Cart",
icon: Icon(Icons.shopping_bag_outlined),
),
BottomNavigationBarItem(
label: "Home",
icon: Icon(Icons.person_3_outlined),
),
],
),
),
For controller
no refresh()
or update()
is needed. update()
is only needed for updating any list
.