latestVersion

mail@pastecode.io avatar
unknown
dart
2 years ago
7.9 kB
1
Indexable
Never
import 'package:base_architecture/core/shared_widgets/categories_rail.dart';
import 'package:base_architecture/presentation/notifiers/categories_notifier/categories_notifier.dart';
import 'package:base_architecture/presentation/notifiers/theme_notifier.dart';
import 'package:base_architecture/presentation/pages/home_page/widgets/home_book_rails.dart';
import 'package:base_architecture/presentation/pages/main/main_page.dart';
import 'package:base_architecture/presentation/resources/color_manager.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LatestVersions extends StatefulWidget {
  const LatestVersions({Key? key}) : super(key: key);

  @override
  State<LatestVersions> createState() => _LatestVersionsState();
}

class _LatestVersionsState extends State<LatestVersions> {
  ThemeNotifier themeNotifier =
      Provider.of<ThemeNotifier>(navigatorKey.currentContext!, listen: true);
  CategoriesNotifier categoriesNotifier = Provider.of<CategoriesNotifier>(
      navigatorKey.currentContext!,
      listen: true);
  @override
  void initState() {
    // TODO: implement initState
    categoriesNotifier.resetPageIndex();
    categoriesNotifier.categories.clear();

    getData();

    categoriesNotifier.newestScrollController.addListener(_scrollListener);
    super.initState();
  }

  void getData() {
    categoriesNotifier.selections.id = null;
    categoriesNotifier.selections.type = 'newest';
    categoriesNotifier.selections.keyword = '';
    categoriesNotifier.getAllCategories(categoriesNotifier.selections);
  }

  void _scrollListener() {
    if (categoriesNotifier.hasNextPage == true &&
        categoriesNotifier.isFirstLoadRunning == false &&
        categoriesNotifier.isLoadMoreRunning == false &&
        categoriesNotifier.newestScrollController.position.pixels ==
            categoriesNotifier
                .newestScrollController.position.maxScrollExtent) {
      categoriesNotifier.setIsLoadMoreRunning(true);

      Future.delayed(Duration(milliseconds: 500)).then((value) => getData());
    }
  }

  @override
  Widget build(BuildContext context) => Consumer<CategoriesNotifier>(
        builder: (BuildContext context, CategoriesNotifier notifier,
                Widget? child) =>
            categoriesNotifier.isLoading
                ? const SizedBox()
                : DefaultTabController(
                    initialIndex: categoriesNotifier.recentTabIndex,
                    length: categoriesNotifier.mainCategoriesTab.length,
                    child: Scaffold(
                      backgroundColor: themeNotifier.getTheme().primaryColor,
                      appBar: _buildAppBar(themeNotifier),
                      body: _buildList(),
                    ),
                  ),
      );

  Widget _buildList() => Consumer<CategoriesNotifier>(
        builder: (BuildContext context, CategoriesNotifier notifier,
                Widget? child) =>
            notifier.mainCategories.data == null
                ? const Center(
                    child: CircleAvatar(
                      radius: 50,
                      backgroundColor: Colors.red,
                    ),
                  )
                : Column(
                    children: [
                      Expanded(
                        child: ListView.builder(
                          controller: notifier.newestScrollController,
                          padding: const EdgeInsets.only(bottom: 80),
                          shrinkWrap: true,
                          itemCount: notifier.categories.length,
                          itemBuilder: (context, indexPath) => CategoryRails(
                            category: notifier.categories,
                            categoryId: notifier.categories[indexPath].id!,
                            categoryType: 'newest',
                            title: notifier.categories[indexPath]
                                    .translatedName!.catNameAr ??
                                "NA",
                            moreBooks: notifier
                                .categories[indexPath].mainCategoryBooks!,
                            railIndex: indexPath,
                            sub_category: false,
                          ),
                        ),
                      ),
                      if (notifier.isLoadMoreRunning == true)
                        Padding(
                          padding: const EdgeInsets.only(
                            top: 20,
                            bottom: 40,
                          ),
                          child: Center(
                            child: CircularProgressIndicator(
                              color: HexColor.fromHex('#E83B69'),
                            ),
                          ),
                        ),
                    ],
                  ),
      );
}

AppBar _buildAppBar(ThemeNotifier themeNotifier) => AppBar(
      backgroundColor: themeNotifier.getTheme().primaryColor,
      elevation: 0.0,
      automaticallyImplyLeading: false,
      title: Consumer<CategoriesNotifier>(
        builder: (BuildContext context, CategoriesNotifier notifier,
                Widget? child) =>
            notifier.mainCategoriesTab.isEmpty
                ? const Center(
                    child: CircleAvatar(),
                  )
                : TabBar(
                    onTap: (value) {
                      notifier.setRecentTabIndex(value);
                      notifier.selections.id =
                          notifier.mainCategoriesTab[value].id;
                      notifier.resetPageIndex();
                      notifier.categories.clear();
                      notifier.getAllCategories(notifier.selections);
                    },
                    padding: const EdgeInsets.symmetric(horizontal: 12),
                    labelPadding: const EdgeInsets.symmetric(horizontal: 5),
                    automaticIndicatorColorAdjustment: false,
                    isScrollable: true,
                    indicatorWeight: 0.1,
                    unselectedLabelColor: themeNotifier.getTheme().hintColor,
                    unselectedLabelStyle: themeNotifier
                        .getTheme()
                        .textTheme
                        .headline4!
                        .copyWith(
                            fontSize: 16,
                            color: themeNotifier.getTheme().hintColor),
                    labelStyle: themeNotifier
                        .getTheme()
                        .textTheme
                        .headline4!
                        .copyWith(
                            fontSize: 16,
                            color: themeNotifier.getTheme().hoverColor),
                    tabs: List.generate(
                      notifier.mainCategoriesTab.length,
                      (index) => Tab(
                        child: Container(
                          margin: const EdgeInsets.only(top: 10),
                          padding: const EdgeInsets.symmetric(
                              vertical: 8, horizontal: 6),
                          decoration: BoxDecoration(
                            color: notifier.recentTabIndex == index
                                ? HexColor.fromHex('#001B34')
                                : themeNotifier.getTheme().primaryColor,
                            borderRadius:
                                const BorderRadius.all(Radius.circular(10)),
                            border: Border.all(
                                color: themeNotifier.getTheme().hintColor),
                          ),
                          child:
                              Text(notifier.mainCategoriesTab[index].nameAr!),
                        ),
                      ),
                    ),
                  ),
      ),
    );