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/main/main_page.dart';
import 'package:base_architecture/presentation/resources/color_manager.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class AllCategories extends StatefulWidget {
const AllCategories({Key? key}) : super(key: key);
@override
State<AllCategories> createState() => _AllCategoriesState();
}
class _AllCategoriesState extends State<AllCategories> {
final themeNotifier =
Provider.of<ThemeNotifier>(navigatorKey.currentContext!, listen: true);
final categoriesNotifier = Provider.of<CategoriesNotifier>(
navigatorKey.currentContext!,
listen: true,
);
@override
void initState() {
// TODO: implement initState
categoriesNotifier.resetPageIndex();
categoriesNotifier.categories.clear();
getData();
categoriesNotifier.categoriesScrollController.addListener(_scrollListener);
super.initState();
}
void getData() {
categoriesNotifier.selections.id = null;
categoriesNotifier.selections.type = 'category';
categoriesNotifier.selections.keyword = '';
categoriesNotifier.getAllCategories(categoriesNotifier.selections);
}
void _scrollListener() {
if (categoriesNotifier.hasNextPage == true &&
categoriesNotifier.isFirstLoadRunning == false &&
categoriesNotifier.isLoadMoreRunning == false &&
categoriesNotifier.categoriesScrollController.position.pixels ==
categoriesNotifier
.categoriesScrollController.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.categoriesTabIndex,
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 SizedBox()
: Column(
children: [
Expanded(
child: ListView.builder(
controller: notifier.categoriesScrollController,
padding: const EdgeInsets.only(bottom: 80),
shrinkWrap: true,
itemCount: notifier.categories.length,
itemBuilder: (context, indexPath) => CategoryRails(
category: notifier.categories,
categoryType: 'category',
categoryId: notifier.categories[indexPath].id!,
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) =>
notifier.mainCategoriesTab.isEmpty
? const SizedBox()
: TabBar(
onTap: (value) {
notifier.setCategoriesTabIndex(value);
notifier.selections.type = 'category';
notifier.selections.id =
notifier.mainCategoriesTab[value].id;
notifier.resetPageIndex();
notifier.categories.clear();
notifier.getAllCategories(
notifier.selections,
);
},
padding: EdgeInsets.zero,
labelPadding: const EdgeInsets.symmetric(horizontal: 4),
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.categoriesTabIndex == 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!),
),
),
),
),
),
);