Skip to content

Instantly share code, notes, and snippets.

@diefferson
Created January 24, 2024 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diefferson/08b3cb8fb1688233b9f9704c43f57203 to your computer and use it in GitHub Desktop.
Save diefferson/08b3cb8fb1688233b9f9704c43f57203 to your computer and use it in GitHub Desktop.
MVPA (BLOC) Example
import 'package:base/base.dart';
import 'package:flutter/material.dart';
import 'package:module_home/module_home.dart';
import 'package:module_home/src/analytics/help_events.dart';
import 'package:module_home/src/presentation/widgets/faq_category_item.dart';
import 'faq_page_actions.dart';
import 'faq_page_presenter.dart';
class FaqPage extends StatefulWidget {
const FaqPage({super.key});
static Future push({required BuildContext context}) {
return NoodleNavigator.push(context, const FaqPage());
}
@override
State createState() => _FaqPageState();
}
class _FaqPageState extends BaseState<FaqPage, FaqPagePresenter> with FaqPageActions {
@override
AnalyticsEvent? get analyticsScreenEvent => HelpEvents.faqScreen();
@override
Widget build(BuildContext context) {
return NoodleScaffold(
title: HomeStrings.of(context).commonQuestions,
backgroundColor: AppColors.of(context).background,
onRefresh: presenter.refresh,
appBarColor: AppColors.of(context).backgroundLight,
appBarBottom: _appBarBottom(),
appBarBottomHeight: BehaviorSubject.seeded(220),
body: _body(),
);
}
Widget _appBarBottom() {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
NoodleText(
HomeStrings.of(context).anyQuestion,
style: TextStyle(
fontSize: FontSize.s32,
fontWeight: FontWeight.w700,
color: AppColors.of(context).textDefault,
),
),
const VSpace(8),
NoodleText(
HomeStrings.of(context).hereYouFindAnswers,
style: TextStyle(
fontSize: FontSize.s14,
fontWeight: FontWeight.w400,
color: AppColors.of(context).textDefault,
),
),
const VSpace(24),
NoodleSearchField(
hint: AppStrings.of(context).search,
margin: EdgeInsets.zero,
onChanged: presenter.search,
),
const VSpace(30),
],
),
);
}
Widget _body() {
return NoodleStreamBuilder<List<FaqCategory>>(
stream: presenter.faq,
defaultValue: [FaqCategory(), FaqCategory()],
builder: (context, categories) {
return ListView.builder(
itemCount: categories.length,
shrinkWrap: true,
padding: const EdgeInsets.only(top: 24, bottom: 56),
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
return FaqCategoryItem(
category: categories[index],
action: () {
goToFaqCategory(
categories[index],
);
},
);
},
);
},
);
}
}
import 'package:base/base.dart';
import 'package:module_home/src/analytics/help_events.dart';
import 'package:module_home/src/domain/model/faq_category.dart';
import 'package:module_home/src/presentation/pages/faq_category/faq_category_page.dart';
import 'faq_page.dart';
import 'faq_page_presenter.dart';
mixin FaqPageActions on BaseState<FaqPage, FaqPagePresenter> {
Future goToFaqCategory(FaqCategory category) async {
noodleAnalytics.logEvent(HelpEvents.faqCategoryClicked(category.category));
await FaqCategoryPage.push(context: context, faqCategory: category);
}
}
import 'package:base/base.dart';
import 'package:module_home/src/domain/interactor/get_faq_use_case.dart';
import 'package:module_home/src/domain/model/faq_category.dart';
import 'faq_page_actions.dart';
class FaqPagePresenter extends BasePresenter<FaqPageActions> {
FaqPagePresenter(this._getFaqUseCase);
final GetFaqUseCase _getFaqUseCase;
List<FaqCategory> _faq = [];
final _filteredFaq = BehaviorSubject<List<FaqCategory>?>();
Stream<List<FaqCategory>?> get faq => _filteredFaq.stream;
@override
void init() {
super.init();
_getFaq();
}
@override
Future refresh() async {
_getFaq();
}
void _getFaq() {
_faq = [];
_filteredFaq.safeAdd(null);
_getFaqUseCase.execute().onSuccess((data) {
_faq = data;
_filteredFaq.safeAdd(_faq);
});
}
void search(String query) {
_filteredFaq.safeAdd(
_faq
.where((cat) =>
cat.category.toLowerCase().contains(query.toLowerCase()) ||
cat.description.toLowerCase().contains(query.toLowerCase()))
.toList(),
);
}
@override
void dispose() {
_filteredFaq.safeClose();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment