Untitled

 avatar
unknown
dart
3 years ago
5.7 kB
2
Indexable
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:iketfaa_delivery/App/Common/Modules/MyProfile/controller/my_profile_controller.dart';
import 'package:iketfaa_delivery/App/Common/Modules/MyProfile/widget/my_profile_header.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Constants/AppColors.dart';
import 'package:iketfaa_delivery/App/Common/Widgets/PostItemWidget.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/UserPostItem.dart';

class MyProfileView extends GetView<MyProfileController> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: 0,
      length: 2,
      child: Scaffold(
        backgroundColor: AppColors.backgroundColor,
        body: NotificationListener<ScrollNotification>(
            onNotification: (ScrollNotification scrollInfo) {
              if (!controller.isLoading.value &&
                  scrollInfo.metrics.pixels ==
                      scrollInfo.metrics.maxScrollExtent &&
                  controller.notCached.value) {
                controller.isLoading.value = true;

                Future.delayed(const Duration(milliseconds: 100), () async {
                  List<UserPostItem> list = <UserPostItem>[];
                  list = await controller.authManager.freelanceAPI
                      .getUserNext12Posts(controller.postsPageIndex.value);
                  controller.isLoading.value = false;

                  if (list.length != 0) {
                    controller.postsPageIndex.value += 1;
                    controller.userPosts.addAll(list);
                  } else {
                    controller.notCached.value = false;
                    controller.postsPageIndex.value = 2;
                  }
                });

                return true;
              }

              return false;
            },
            child: ListView(
              padding: EdgeInsets.zero,
              children: [
                MyProfileHeader(),
                TabBar(
                  isScrollable: false,
                  indicatorColor: AppColors.primary,
                  labelColor: AppColors.black,
                  labelStyle: Get.textTheme.headline6!
                      .copyWith(fontWeight: FontWeight.w500),
                  tabs: [
                    Tab(
                      text: 'posts'.tr,
                    ),
                    Tab(text: 'videos'.tr),
                  ],
                  controller: controller.tabController.value,
                  onTap: (index) {
                    //  controller.currentTabIndex.value = index;
                  },
                ),
                Column(
                  children: [
                    Container(
                      height:
                          (controller.userPosts.length / 3) * Get.width * 0.25 +
                              Get.height * 0.1,
                      child: TabBarView(
                        physics: const NeverScrollableScrollPhysics(),
                        controller: controller.tabController.value,
                        children: [
                          Obx(
                            () => controller.userPosts.isEmpty
                                ? Center(
                                    child: Text(
                                      "You don't have any posts yet".tr,
                                      style: Get.textTheme.headline5,
                                    ),
                                  )
                                : GridView.builder(
                                    scrollDirection: Axis.vertical,
                                    padding: EdgeInsets.zero,
                                    shrinkWrap: true,
                                    physics:
                                        const NeverScrollableScrollPhysics(),
                                    addAutomaticKeepAlives: true,
                                    addRepaintBoundaries: false,
                                    gridDelegate:
                                        const SliverGridDelegateWithFixedCrossAxisCount(
                                      crossAxisCount: 3,
                                    ),
                                    cacheExtent: Get.width * 0.25,
                                    itemCount: controller.userPosts.length,
                                    itemBuilder: (context, indexPath) {
                                      return PostItemWidget(
                                        postItem:
                                            controller.userPosts[indexPath],
                                      );
                                    },
                                  ),
                          ),
                          const Center(
                            child: Text('ok'),
                          ),
                        ],
                      ),
                    ),
                    Visibility(
                      visible: controller.notCached.value,
                      child: Container(
                        margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
                        width: Get.width,
                        height: 30.0,
                        child: Visibility(
                            visible: controller.isLoading.value,
                            child: const CupertinoActivityIndicator()),
                      ),
                    )
                  ],
                ),
              ],
            )),
      ),
    );
  }
}