forked from eatvio/eatvio_app
99 lines
3.3 KiB
Dart
99 lines
3.3 KiB
Dart
import 'package:easy_debounce/easy_debounce.dart';
|
|
import 'package:eatvio_app/core/locator_service.dart';
|
|
import 'package:eatvio_app/core/widgets/loader.dart';
|
|
import 'package:eatvio_app/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart';
|
|
import 'package:eatvio_app/features/profile/presentation/bloc/sub_cubit/sub_state.dart';
|
|
import 'package:eatvio_app/features/profile/presentation/widgets/sub_widget.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
enum SubType { Subscribers, Subscriptions }
|
|
|
|
class SubsScreen extends StatefulWidget {
|
|
final SubType subType;
|
|
final String slug;
|
|
|
|
const SubsScreen({
|
|
super.key,
|
|
required this.slug,
|
|
required this.subType,
|
|
});
|
|
|
|
@override
|
|
State<SubsScreen> createState() => _SubsScreenState();
|
|
}
|
|
|
|
class _SubsScreenState extends State<SubsScreen> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocProvider<SubCubit>(
|
|
create: (BuildContext context) => widget.subType == SubType.Subscriptions
|
|
? (sl<SubCubit>()
|
|
..getSubscriptions(
|
|
widget.slug,
|
|
))
|
|
: (sl<SubCubit>()
|
|
..getSubscribers(
|
|
widget.slug,
|
|
)),
|
|
child: BlocBuilder<SubCubit, SubState>(
|
|
builder: (BuildContext context, SubState state) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
bottom: PreferredSize(
|
|
preferredSize: Size(MediaQuery.of(context).size.width, 40),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: CupertinoSearchTextField(
|
|
prefixIcon: Icon(Icons.search),
|
|
placeholder: 'Поиск',
|
|
onChanged: (query) {
|
|
if (query.length > 2) {
|
|
EasyDebounce.debounce(
|
|
'searchSubs',
|
|
Duration(milliseconds: 300),
|
|
() {
|
|
widget.subType == SubType.Subscriptions
|
|
? (context.read<SubCubit>().getSubscriptions(widget.slug, query))
|
|
: (context.read<SubCubit>().getSubscribers(widget.slug, query));
|
|
},
|
|
);
|
|
} else {
|
|
widget.subType == SubType.Subscriptions
|
|
? (context.read<SubCubit>().getSubscriptions(widget.slug))
|
|
: (context.read<SubCubit>().getSubscribers(widget.slug));
|
|
}
|
|
},
|
|
),
|
|
),
|
|
),
|
|
title: Text(
|
|
widget.subType == SubType.Subscriptions ? 'Подписки' : 'Подписчики',
|
|
),
|
|
),
|
|
body: _buildStates(state),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
_buildStates(SubState state) {
|
|
if (state is SubStateLoading) {
|
|
return Loader();
|
|
}
|
|
if (state is SubStateError) {
|
|
return Text(state.errorMessage);
|
|
}
|
|
if (state is SubStateLoaded) {
|
|
return ListView.builder(
|
|
itemBuilder: (context, index) => SubWidget(
|
|
subModel: state.subList[index],
|
|
),
|
|
itemCount: state.subList.length,
|
|
);
|
|
}
|
|
}
|
|
}
|