Создание коллекций
This commit is contained in:
@@ -1,75 +1,111 @@
|
||||
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:flutter/rendering.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';
|
||||
import 'package:remever/services/collection/collections_interface.dart';
|
||||
|
||||
@RoutePage()
|
||||
class CollectionScreen extends StatelessWidget with GetItMixin {
|
||||
CollectionScreen({super.key});
|
||||
class CollectionScreen extends StatefulWidget {
|
||||
const CollectionScreen({super.key});
|
||||
|
||||
/// Флаг что надо показывать Fab
|
||||
bool get _showFab {
|
||||
return watchOnly<CollectionData, bool>((CollectionData d) => d.showFAB);
|
||||
@override
|
||||
State<CollectionScreen> createState() => _CollectionScreenState();
|
||||
}
|
||||
|
||||
class _CollectionScreenState extends State<CollectionScreen> {
|
||||
/// Контроллер скролла для коллекции
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
|
||||
/// Показ FAB'а
|
||||
bool _showFab = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initScrollListener();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Инициализация слушателя скролла
|
||||
void _initScrollListener() {
|
||||
_scrollController.addListener(() {
|
||||
setState(() {
|
||||
_showFab =
|
||||
_scrollController.position.userScrollDirection ==
|
||||
ScrollDirection.reverse
|
||||
? false
|
||||
: true;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@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),
|
||||
),
|
||||
);
|
||||
},
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.bg,
|
||||
appBar: const CollectionsAppBar(),
|
||||
body: _buildMain(context),
|
||||
floatingActionButton: AnimatedOpacity(
|
||||
opacity: _showFab ? 1.0 : 0.0,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: FloatingActionButton(
|
||||
backgroundColor: AppColors.primary,
|
||||
onPressed: () => context.pushRoute(CrudCollectionRoute()),
|
||||
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(),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
return Column(
|
||||
children: [
|
||||
const CollectionsFilters(),
|
||||
Expanded(
|
||||
child: StreamBuilder<List<Collection>>(
|
||||
stream: getIt<CollectionsInterface>().getCollectionsList(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const _LoadingList();
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return const _ErrorList();
|
||||
}
|
||||
|
||||
final collections = snapshot.data;
|
||||
if (collections == null || collections.isEmpty) {
|
||||
return const _EmptyList();
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
controller: _scrollController,
|
||||
itemCount: collections.length,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16).r,
|
||||
itemBuilder:
|
||||
(context, index) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8).r,
|
||||
child: CollectionCard(collection: collections[index]),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -111,54 +147,3 @@ class _EmptyList extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user