others

mail@pastecode.io avatar
unknown
dart
a year ago
6.5 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 OthersPage extends StatefulWidget {
  const OthersPage({Key? key}) : super(key: key);

  @override
  State<OthersPage> createState() => _OthersPageState();
}

class _OthersPageState extends State<OthersPage> {
  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.othersScrollController.addListener(_scrollListener);
    super.initState();
  }

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

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

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

  @override
  Widget build(BuildContext context) => DefaultTabController(
        initialIndex: categoriesNotifier.otherTabIndex,
        length: categoriesNotifier.otherTabs.length,
        child: Scaffold(
          backgroundColor: themeNotifier.getTheme().primaryColor,
          appBar: _buildAppBar(themeNotifier),
          body: _buildList(),
        ),
      );

  Widget _buildList() => Consumer<CategoriesNotifier>(
        builder: (BuildContext context, CategoriesNotifier notifier,
                Widget? child) =>
            Column(
          children: [
            Expanded(
              child: ListView.builder(
                controller: notifier.othersScrollController,
                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: 'authors',
                  title:
                      notifier.categories[indexPath].translatedName!.catNameAr ??
                          "NA",
                  moreBooks: notifier.categories[indexPath].mainCategoryBooks!,
                  railIndex: indexPath,
                  sub_category: true,
                ),
              ),
            ),
            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) =>
              TabBar(
        onTap: (value) {
          String? selected;
          switch (notifier.otherTabs[value]) {
            case 'الكُتّاب':
              selected = 'authors';
              break;
            case 'القُرّاء':
              selected = 'readers';
              break;
            case 'المترجمين':
              selected = 'translators';
              break;
            case 'دور النشر':
              selected = 'publishers';
              break;
          }
          print(selected);
          notifier.setOtherTabIndex(value);
          notifier.selections.type = selected ?? '';
          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.otherTabs.length,
          (index) {
            return Tab(
              child: Container(
                margin: const EdgeInsets.only(top: 10),
                padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 6),
                decoration: BoxDecoration(
                  color: notifier.otherTabIndex == index
                      ? HexColor.fromHex('#001B34')
                      : themeNotifier.getTheme().primaryColor,
                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                  border: Border.all(color: themeNotifier.getTheme().hintColor),
                ),
                child: Text(notifier.otherTabs[index]),
              ),
            );
          },
        ),
      ),
    ));