Search code examples
flutterfirebasedartblocflutter-getx

GetX state management cannot update value on SliverPersistentHeader


I face some issues with Getx state management, when I wrap CustomScrollView on SliverPersistentHeader then I implement list view on it then I make some even on the list but it is not updated. Here is my controller:

class SFController extends GetxController {
  int currentIndex = 0;
  onSeletedTab(int index) {
    currentIndex = index;
    update();
  }
}

Here is my UI

GetBuilder(
          init: controller,
          builder: (_) {
            return CustomScrollView(slivers: [
              SliverAppBar(),
              SliverPersistentHeader(
                pinned: true,
                delegate: _HeaderSliver(controller),
              ),
            ]);
          }),

Here is _HeaderSliver:

class _HeaderSliver extends SliverPersistentHeaderDelegate {
  final SFController controller;
  _HeaderSliver(this.controller);

@override
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
return Stack(
  children: [
    Positioned(
      bottom: 0.0,
      left: 0.0,
      right: 0.0,
      top: 0.0,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        height: _maxHeaferExtent,
        color: Colors.white,
        child: Row(
          children: [
            Expanded(
              child: Container(
                alignment: Alignment.center,
                height: 35.0,
                color: Colors.white,
                child: ListView.separated(
                  padding: const EdgeInsets.only(
                  left: 4.0, right: 4.0, bottom:        4.0, top: 0.0),
                  scrollDirection: Axis.horizontal,
                  itemCount: 3,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      onTap: () {
                        log("Index:");
                        controller.onSeletedTab(index);
                      },
                      child: Container(
                        padding: const EdgeInsets.only(left: 10.0, right: 10.0),
                        height: 36.0,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: controller.indeTest == index ? 
                           AppColor.secondaryColor : AppColor.neutral25,
                          borderRadius: BorderRadius.circular(24.0),
                        ),
                        child: Text(
                          controller.stringTitle[index],
                          style: Theme.of(context).textTheme.bodyText1?.copyWith(
                                fontSize: 14,
                                color: controller.indeTest == index ? 
                                 AppColor.white : AppColor.neutral900,
                                fontWeight: controller.indeTest == index ? 
                              FontWeight.w600 : FontWeight.w400,
                              ),
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) =>
                      const Padding(padding: EdgeInsets.only(left: 10.0)),
                ),
              ),
            ),
            Container(
              padding: const EdgeInsets.only(left: 0.0, right: 4.0, bottom: 8.0, 
              top: 0.0),
              alignment: Alignment.centerRight,
              height: 36.0,
              color: Colors.white,
              child: Text(
                "Select",
                style: Theme.of(context).textTheme.bodyText1?.copyWith(
                      fontSize: 12,
                      color: AppColor.secondaryColor,
                      fontWeight: FontWeight.w700,
                    ),
              ),
            )
          ],
        ),
      ),
    )
  ],
);
}

@override
double get maxExtent => _maxHeaferExtent;

@override
double get minExtent => _maxHeaferExtent;

@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) => false;
}

I try calling method on controller for update item on SliverPersistentHeader but it not working.


Solution

  • In the init parameter of GetBuilder, you can try putting SFController() instead of controller. Because by using controller, you are directly accessing(finding) it.