Skip to content

Instantly share code, notes, and snippets.

@wisnuwiry
Created June 3, 2020 04:10
Show Gist options
  • Save wisnuwiry/7da76b7c2286f9a8df5e841b9baa8bfc to your computer and use it in GitHub Desktop.
Save wisnuwiry/7da76b7c2286f9a8df5e841b9baa8bfc to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:rxdart/rxdart.dart';
import '../../data/data.dart';
part 'product_bycategory_event.dart';
part 'product_bycategory_state.dart';
class ProductBycategoryBloc
extends Bloc<ProductBycategoryEvent, ProductBycategoryState> {
final ProductRepository repository;
ProductBycategoryBloc({@required this.repository});
@override
ProductBycategoryState get initialState => ProductBycategoryLoading();
@override
Stream<ProductBycategoryState> transformEvents(
Stream<ProductBycategoryEvent> events,
Stream<ProductBycategoryState> Function(ProductBycategoryEvent) next,
) {
return super.transformEvents(
events.debounceTime(
Duration(milliseconds: 500),
),
next,
);
}
@override
Stream<ProductBycategoryState> mapEventToState(
ProductBycategoryEvent event,
) async* {
if (event is GetProductByCategoryEvent) {
yield* _mapEventStateGetProduct(event.categoryId, event.perPage, 1);
} else if (event is GetProductMoreByCategoryEvent) {
yield* _mapEventStateGetProduct(
event.categoryId, event.perPage, event.page);
}
}
Stream<ProductBycategoryState> _mapEventStateGetProduct(
int categoryId, int perPage, int page) async* {
try {
ProductBycategoryLoaded currentData;
if (state is ProductBycategoryLoaded) {
currentData = (state as ProductBycategoryLoaded);
}
if (page > 1) {
yield ProductBycategoryMoreLoading();
} else {
yield ProductBycategoryLoading();
}
var newData =
await repository.getProductByCategory(categoryId, perPage, page);
var resData;
if (currentData != null &&
currentData.categoriesId == categoryId) {
resData = currentData.data..addAll(newData);
} else {
resData = newData;
}
yield ProductBycategoryLoaded(
data: resData, page: page, categoriesId: categoryId);
} catch (e) {
yield ProductBycategoryError();
}
}
}
part of 'product_bycategory_bloc.dart';
abstract class ProductBycategoryEvent extends Equatable {
const ProductBycategoryEvent();
}
class GetProductByCategoryEvent extends ProductBycategoryEvent {
final int categoryId;
final int perPage;
GetProductByCategoryEvent({
@required this.categoryId,
this.perPage = 10,
});
@override
List<Object> get props => [categoryId, perPage];
}
class GetProductMoreByCategoryEvent extends ProductBycategoryEvent {
final int categoryId;
final int perPage;
final int page;
GetProductMoreByCategoryEvent({
@required this.categoryId,
this.perPage = 10,
@required this.page,
});
@override
List<Object> get props => [categoryId, perPage, page];
}
part of 'product_bycategory_bloc.dart';
abstract class ProductBycategoryState extends Equatable {
const ProductBycategoryState();
@override
List<Object> get props => [];
}
class ProductBycategoryLoading extends ProductBycategoryState {}
class ProductBycategoryMoreLoading extends ProductBycategoryState {}
class ProductBycategoryLoaded extends ProductBycategoryState {
final List<Product> data;
final int page;
final int categoriesId;
ProductBycategoryLoaded(
{@required this.data, @required this.categoriesId, this.page});
@override
List<Object> get props => [data, categoriesId, page];
}
class ProductBycategoryError extends ProductBycategoryState {}
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../shared/shared.dart';
import '../../../widgets/molecules/molecules.dart';
import '../../../widgets/organisms/order/card_floating_button_org.dart';
import '../../../widgets/organisms/organisms.dart';
import '../../../widgets/templates/templates.dart';
import '../../home/home.dart';
import '../../order/order.dart';
import '../bloc/bloc.dart';
import '../data/data.dart';
class ProductCategoryPage extends StatelessWidget {
final int initialIndex;
final int categoryId;
const ProductCategoryPage(
{Key key, this.initialIndex = 0, @required this.categoryId})
: super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => ProductBycategoryBloc(
repository:
ProductRepository(productApiProvider: ProductApiProvider())),
child: _ProductCategoryPage(
categoryId: categoryId,
initialIndex: initialIndex,
),
);
}
}
class _ProductCategoryPage extends StatefulWidget {
final int initialIndex;
final int categoryId;
const _ProductCategoryPage(
{Key key, this.initialIndex = 0, @required this.categoryId})
: super(key: key);
@override
_ProductCategoryPageState createState() => _ProductCategoryPageState();
}
class _ProductCategoryPageState extends State<_ProductCategoryPage>
with SingleTickerProviderStateMixin {
TextEditingController _textEditingController = TextEditingController();
ProductBycategoryBloc _bloc;
List<CategoriesProduct> _categories;
@override
void initState() {
_bloc = BlocProvider.of<ProductBycategoryBloc>(context);
_bloc.add(GetProductByCategoryEvent(categoryId: widget.categoryId));
super.initState();
}
Widget _buildCategoriesMenu() {
return AppBarCategoryTabMenuOrg(
categories: _categories.map((f) => f.name).toList(),
);
}
@override
Widget build(BuildContext context) {
return BlocBuilder<CategoriesProductBloc, CategoriesProductState>(
builder: (context, state) {
if (state is LoadedCategoriesProductState) {
_categories = state.data;
return _buildBody();
} else {
return SizedBox();
}
},
);
}
Widget _buildBody() {
return BlocBuilder<ProductBycategoryBloc, ProductBycategoryState>(
builder: (context, state) {
return DefaultTabController(
length: _categories.length,
initialIndex: widget.initialIndex,
child: ListCategoryProductTemplate(
appbar: AppBarSearchOrg(
controller: _textEditingController,
context: context,
validate: true,
bottomHeight: 50,
bottom: _buildCategoriesMenu())
.appBar(),
listProduct: _buildListPageView(),
fab: _buildFloatingButton(state),
),
);
},
);
}
Widget _buildListPageView() {
return TabBarView(
children: _categories.map((item) => _buildListProduct(item.id)).toList(),
);
}
Widget _buildListProduct(int categoryId) {
return NotificationListener(
onNotification: (notificationInfo) {
if (notificationInfo is ScrollUpdateNotification) {
if (notificationInfo.depth != 1) {
BlocProvider.of<FabBloc>(context)
.add(GetChangeFabEvent(isVisible: false));
}
} else {
BlocProvider.of<FabBloc>(context)
.add(GetChangeFabEvent(isVisible: true));
}
return true;
},
child: BlocBuilder<FabBloc, FabState>(
builder: (context, state) {
return SingleChildScrollView(
padding: EdgeInsets.fromLTRB(15, 15, 15, _getPaddingBottom(state)),
child: BlocBuilder<ProductBycategoryBloc, ProductBycategoryState>(
builder: (context, state) {
if (state is ProductBycategoryLoaded) {
return ListProductOrg(listProduct: state.data);
} else {
return ShimmerListProductMol(count: 4);
}
},
),
);
},
),
);
}
Widget _buildFloatingButton(ProductBycategoryState state) {
if (state is ProductBycategoryLoaded) {
return CardFloatingButtonOrg(
market: 'Pasar GunungPati',
totalPrice: 100000,
unit: 10,
onPressed: () {
Navigator.of(context).push(SlideTransitionPage(child: OrderScreen()));
},
);
} else {
return SizedBox();
}
}
double _getPaddingBottom(FabState state) {
double height = 15;
if (state is FabVisibleState) {
height = 90;
} else {
height = 15;
}
return height;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment