165 lines
5.0 KiB
Dart
165 lines
5.0 KiB
Dart
import 'package:auto_route/auto_route.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:get_it_mixin/get_it_mixin.dart';
|
|
import 'package:remever/common/resources.dart';
|
|
import 'package:remever/common/widgets/typography.dart';
|
|
import 'package:remever/components/extensions/context.dart';
|
|
import 'package:remever/components/notifiers/home_screen_data.dart';
|
|
import 'package:remever/database/database.dart';
|
|
import 'package:remever/database/tables.dart';
|
|
import 'package:remever/inject.dart';
|
|
import 'package:remever/router.gr.dart';
|
|
import 'package:remever/screens/collections/cubit/collection_cubit.dart';
|
|
import 'package:remever/screens/collections/widgets/collection_card.dart';
|
|
import 'package:remever/screens/collections/widgets/collections_app_bar.dart';
|
|
import 'package:remever/screens/collections/widgets/collections_filters.dart';
|
|
|
|
@RoutePage()
|
|
class CollectionScreen extends StatelessWidget with GetItMixin {
|
|
CollectionScreen({super.key});
|
|
|
|
/// Флаг что надо показывать Fab
|
|
bool get _showFab {
|
|
return watchOnly<CollectionData, bool>((CollectionData d) => d.showFAB);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocProvider<CollectionCubit>(
|
|
create: (context) => CollectionCubit(),
|
|
child: Scaffold(
|
|
backgroundColor: AppColors.bg,
|
|
appBar: const CollectionsAppBar(),
|
|
body: _buildMain(context),
|
|
floatingActionButton: Builder(
|
|
builder: (BuildContext context) {
|
|
return AnimatedOpacity(
|
|
opacity: _showFab ? 1.0 : 0.0,
|
|
duration: const Duration(milliseconds: 200),
|
|
child: FloatingActionButton(
|
|
backgroundColor: AppColors.primary,
|
|
onPressed: () {
|
|
context.pushRoute(CrudCollectionRoute());
|
|
},
|
|
// context.read<HomeCubit>().toCrudCollection(CrudType.CREATE),
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
///
|
|
/// Построение основго экрана
|
|
///
|
|
Widget _buildMain(BuildContext context) {
|
|
return BlocBuilder<CollectionCubit, CollectionState>(
|
|
builder: (context, state) {
|
|
return Column(
|
|
children: [
|
|
CollectionsFilters(),
|
|
state.when(
|
|
loading: () => _LoadingList(),
|
|
data: () => _CollectionList(),
|
|
empty: () => _EmptyList(),
|
|
error: () => _ErrorList(),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class _LoadingList extends StatelessWidget {
|
|
const _LoadingList();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SizedBox(
|
|
height: MediaQuery.sizeOf(context).height / 2,
|
|
child: Center(child: CircularProgressIndicator(color: AppColors.primary)),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ErrorList extends StatelessWidget {
|
|
const _ErrorList();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SizedBox(
|
|
height: MediaQuery.sizeOf(context).height / 2,
|
|
child: Center(
|
|
child: AppTypography('Произошла ошибка при загрузке данных'),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _EmptyList extends StatelessWidget {
|
|
const _EmptyList();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SizedBox(
|
|
height: MediaQuery.sizeOf(context).height / 2,
|
|
child: Center(child: AppTypography('Нет доступных коллекций')),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _CollectionList extends StatelessWidget {
|
|
const _CollectionList();
|
|
|
|
// @override
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
print('build _CollectionList');
|
|
final CollectionCubit collectionCubit = context.read<CollectionCubit>();
|
|
collectionCubit.initScrollListener();
|
|
|
|
return Expanded(
|
|
child: StreamBuilder(
|
|
stream: getIt<AppDatabase>().collectionsDao.getCollections(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return Center(
|
|
child: CircularProgressIndicator(color: AppColors.primary),
|
|
);
|
|
}
|
|
|
|
if (snapshot.connectionState == ConnectionState.active ||
|
|
snapshot.connectionState == ConnectionState.done) {
|
|
if (snapshot.hasError) {
|
|
return _ErrorList();
|
|
}
|
|
|
|
if (snapshot.data == null || snapshot.data!.isEmpty) {
|
|
return _EmptyList();
|
|
}
|
|
|
|
return ListView.builder(
|
|
controller: collectionCubit.collectionController,
|
|
itemCount: snapshot.data!.length,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16).r,
|
|
itemBuilder:
|
|
(BuildContext context, int index) => Padding(
|
|
padding: const EdgeInsets.only(bottom: 8).r,
|
|
child: const CollectionCard(),
|
|
),
|
|
);
|
|
}
|
|
|
|
return Center(
|
|
child: CircularProgressIndicator(color: AppColors.primary),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|