step 23 : home screen, repository, provider
This commit is contained in:
@@ -1,17 +1,34 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:app_petty_cash/screen/home/info_account_balance_provider.dart';
|
||||
import 'package:app_petty_cash/screen/home/list_transaksi_home_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../app/route.dart';
|
||||
import '../../model/info_account_balance.dart';
|
||||
import '../../model/list_transaksi_model.dart';
|
||||
import '../../provider/current_info_account_balance_provider.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
import '../../widget/custom_drawer.dart';
|
||||
import '../../widget/history_row_atas_widget.dart';
|
||||
import '../../widget/sankbar_widget.dart';
|
||||
|
||||
class HomeScreen extends HookConsumerWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final M_CompanyID = useState<String>("0");
|
||||
final searchTransaksiHomeLoading = useState<bool>(false);
|
||||
final infoAccountBalanceHomeLoading = useState<bool>(false);
|
||||
final listTransaksiHome = useState<List<ListTransaksiHomeModel>>(
|
||||
List.empty(growable: true),
|
||||
);
|
||||
|
||||
useEffect(() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
final userID = ref.read(currentUserProvider)?.model.M_UserID ?? "0";
|
||||
@@ -27,6 +44,80 @@ class HomeScreen extends HookConsumerWidget {
|
||||
return () {};
|
||||
}, []);
|
||||
|
||||
Future<String> getCompanyID() async {
|
||||
final shared = await SharedPreferences.getInstance();
|
||||
String M_CompanyID = "0";
|
||||
|
||||
if (shared != null) {
|
||||
final bearerString = shared.get(Constant.bearerName).toString();
|
||||
final xmodel = jsonDecode(bearerString);
|
||||
if (xmodel != null) {
|
||||
M_CompanyID = xmodel["model"]["M_CompanyID"];
|
||||
}
|
||||
}
|
||||
|
||||
if (M_CompanyID == "0") {
|
||||
// throw BaseRepositoryException(message: 'Invalid Company ID');
|
||||
}
|
||||
|
||||
return M_CompanyID;
|
||||
}
|
||||
|
||||
useEffect(() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
M_CompanyID.value = await getCompanyID();
|
||||
// info account balance
|
||||
ref
|
||||
.read(infoAccountBalanceHomeProvider.notifier)
|
||||
.infoAccountBalanceHome(M_CompanyID.value);
|
||||
});
|
||||
return () {};
|
||||
}, []);
|
||||
|
||||
// read provider home
|
||||
ref.listen(
|
||||
listTransaksiHomeProvider,
|
||||
(previous, next) {
|
||||
if (next is ListTransaksiHomeStateLoading) {
|
||||
searchTransaksiHomeLoading.value = true;
|
||||
} else if (next is ListTransaksiHomeStateError) {
|
||||
// print(next.message);
|
||||
searchTransaksiHomeLoading.value = false;
|
||||
SanckbarWidget(context, next.message, snackbarType.error);
|
||||
} else if (next is ListTransaksiHomeStateDone) {
|
||||
// print(jsonEncode(next.model));
|
||||
// print(next.model.length);
|
||||
listTransaksiHome.value = next.model;
|
||||
searchTransaksiHomeLoading.value = false;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// read provider info account balance
|
||||
ref.listen(
|
||||
infoAccountBalanceHomeProvider,
|
||||
(previous, next) {
|
||||
if (next is InfoAccountBalanceHomeStateLoading) {
|
||||
infoAccountBalanceHomeLoading.value = true;
|
||||
} else if (next is InfoAccountBalanceHomeStateError) {
|
||||
// print(next.message);
|
||||
infoAccountBalanceHomeLoading.value = false;
|
||||
SanckbarWidget(context, next.message, snackbarType.error);
|
||||
} else if (next is InfoAccountBalanceHomeStateDone) {
|
||||
// print(jsonEncode(next.model));
|
||||
// print(next.model.length);
|
||||
// infoAccountBalanceObject.value = next.model;
|
||||
ref.read(currentInfoAccountBalanceProvider.notifier).state =
|
||||
InfoAccountBalanceModel(
|
||||
kredit: next.model.kredit,
|
||||
debit: next.model.debit,
|
||||
totalAll: next.model.totalAll,
|
||||
);
|
||||
infoAccountBalanceHomeLoading.value = false;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: Constant.getActualYPhone(context: context, y: 30),
|
||||
@@ -44,8 +135,258 @@ class HomeScreen extends HookConsumerWidget {
|
||||
),
|
||||
drawer: CustomDrawer(),
|
||||
body: SafeArea(
|
||||
child: Center(
|
||||
child: Text('Home Screen Content'),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: Constant.getActualYPhone(context: context, y: 36),
|
||||
left: Constant.getActualXPhone(context: context, x: 34),
|
||||
right: Constant.getActualXPhone(context: context, x: 34),
|
||||
),
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
ref
|
||||
.read(infoAccountBalanceHomeProvider.notifier)
|
||||
.infoAccountBalanceHome(M_CompanyID.value);
|
||||
},
|
||||
child: Container(
|
||||
width: Constant.getActualXPhone(context: context, x: 390),
|
||||
height: Constant.getActualYPhone(context: context, y: 844),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
width: Constant.getActualXPhone(context: context, x: 332),
|
||||
height:
|
||||
Constant.getActualYPhone(context: context, y: 173),
|
||||
decoration: BoxDecoration(
|
||||
// color: Colors.black12,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
image: DecorationImage(
|
||||
image: AssetImage('images/background_card.png'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Color.fromRGBO(145, 158, 171, 0.16),
|
||||
offset: Offset(0, 24),
|
||||
blurRadius: 48,
|
||||
),
|
||||
],
|
||||
),
|
||||
//Isi Card
|
||||
child: (infoAccountBalanceHomeLoading.value)
|
||||
? Center(
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
: Container(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Text1 & Text2
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: Constant.getActualYPhone(
|
||||
context: context, y: 17),
|
||||
left: Constant.getActualXPhone(
|
||||
context: context, x: 81),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context, x: 81),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'Account Balance',
|
||||
style: Constant.body1(
|
||||
context: context)
|
||||
.copyWith(
|
||||
fontWeight: FontWeight.w400,
|
||||
color: Constant.textWhite),
|
||||
),
|
||||
Text(
|
||||
ref
|
||||
.watch(
|
||||
currentInfoAccountBalanceProvider)
|
||||
.totalAll
|
||||
.toString(),
|
||||
style: Constant.H4_700V3(
|
||||
context: context)
|
||||
.copyWith(
|
||||
color: Constant.textWhite,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context, y: 27),
|
||||
),
|
||||
|
||||
//Debit dan Kredit
|
||||
Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// Spasi di sebelah kiri "Check In"
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context, x: 20),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'Debit',
|
||||
style: Constant.body1(
|
||||
context: context)
|
||||
.copyWith(
|
||||
fontWeight:
|
||||
FontWeight.w400,
|
||||
color:
|
||||
Constant.textWhite),
|
||||
),
|
||||
Text(
|
||||
ref
|
||||
.watch(
|
||||
currentInfoAccountBalanceProvider)
|
||||
.debit
|
||||
.toString(),
|
||||
style: Constant.H4_700V3(
|
||||
context: context)
|
||||
.copyWith(
|
||||
color:
|
||||
Constant.textWhite),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// SizedBox(
|
||||
// width: Constant.getActualXPhone(
|
||||
// context: context, x: 51),
|
||||
// ),
|
||||
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(
|
||||
right: Constant.getActualXPhone(
|
||||
context: context, x: 28),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'Credit',
|
||||
style: Constant.body1(
|
||||
context: context)
|
||||
.copyWith(
|
||||
fontWeight:
|
||||
FontWeight.w400,
|
||||
color:
|
||||
Constant.textWhite),
|
||||
),
|
||||
Text(
|
||||
ref
|
||||
.watch(
|
||||
currentInfoAccountBalanceProvider)
|
||||
.kredit
|
||||
.toString(),
|
||||
style: Constant.H4_700V3(
|
||||
context: context)
|
||||
.copyWith(
|
||||
color:
|
||||
Constant.textWhite),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context, y: 17),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(context: context, y: 44),
|
||||
),
|
||||
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Text(
|
||||
'Transaksi Terkini',
|
||||
style: Constant.titleH5_600(context: context)
|
||||
.copyWith(color: Constant.textBlack),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(context: context, y: 24),
|
||||
),
|
||||
|
||||
// list transaksi
|
||||
(searchTransaksiHomeLoading.value)
|
||||
? Center(
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
: Container(
|
||||
child: Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: listTransaksiHome.value.length,
|
||||
itemBuilder: (context, idx) {
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(
|
||||
bottom: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 10,
|
||||
),
|
||||
),
|
||||
child: Card(
|
||||
elevation: 2.0,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: Column(
|
||||
// crossAxisAlignment: CrossAxisAlignment.start,
|
||||
// mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
// atas
|
||||
HistoryRowAtasWidget(
|
||||
icon_category_id:
|
||||
listTransaksiHome
|
||||
.value[idx].kategoriid
|
||||
.toString(),
|
||||
icon_category_name:
|
||||
listTransaksiHome
|
||||
.value[idx].kategoriname
|
||||
.toString(),
|
||||
amount: listTransaksiHome
|
||||
.value[idx].amount
|
||||
.toString(),
|
||||
tglTransaksi: listTransaksiHome
|
||||
.value[idx].tanggaltransaksi
|
||||
.toString(),
|
||||
tipe: listTransaksiHome
|
||||
.value[idx].tipe
|
||||
.toString(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'package:app_petty_cash/provider/current_info_account_balance_provider.dart';
|
||||
import 'package:app_petty_cash/repository/home_repository.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../model/info_account_balance.dart';
|
||||
import '../../provider/dio_provider.dart';
|
||||
import '../../repository/base_repository.dart';
|
||||
import 'list_transaksi_home_provider.dart';
|
||||
|
||||
abstract class InfoAccountBalanceHomeState extends Equatable {
|
||||
final DateTime date;
|
||||
const InfoAccountBalanceHomeState(this.date);
|
||||
@override
|
||||
List<Object?> get props => [date];
|
||||
}
|
||||
|
||||
class InfoAccountBalanceHomeStateInit extends InfoAccountBalanceHomeState {
|
||||
InfoAccountBalanceHomeStateInit() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class InfoAccountBalanceHomeStateLoading extends InfoAccountBalanceHomeState {
|
||||
InfoAccountBalanceHomeStateLoading() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class InfoAccountBalanceHomeStateError extends InfoAccountBalanceHomeState {
|
||||
final String message;
|
||||
InfoAccountBalanceHomeStateError({
|
||||
required this.message,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
class InfoAccountBalanceHomeStateDone extends InfoAccountBalanceHomeState {
|
||||
final InfoAccountBalanceModel model;
|
||||
// final String resp;
|
||||
InfoAccountBalanceHomeStateDone({
|
||||
required this.model,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
//notifier
|
||||
class InfoAccountBalanceHomeNotifier
|
||||
extends StateNotifier<InfoAccountBalanceHomeState> {
|
||||
final Ref ref;
|
||||
InfoAccountBalanceHomeNotifier({
|
||||
required this.ref,
|
||||
}) : super(InfoAccountBalanceHomeStateInit());
|
||||
|
||||
void infoAccountBalanceHome(
|
||||
String companyid,
|
||||
) async {
|
||||
try {
|
||||
state = InfoAccountBalanceHomeStateLoading();
|
||||
final dio = ref.read(dioProvider);
|
||||
final resp = await HomeRepository(dio: dio).getInfoAccountBalance(
|
||||
companyid,
|
||||
);
|
||||
state = InfoAccountBalanceHomeStateDone(model: resp);
|
||||
// list transaksi home
|
||||
ref.read(listTransaksiHomeProvider.notifier).listTransaksiHome(companyid);
|
||||
} catch (e) {
|
||||
if (e is BaseRepositoryException) {
|
||||
state = InfoAccountBalanceHomeStateError(message: e.message.toString());
|
||||
} else {
|
||||
state = InfoAccountBalanceHomeStateError(message: e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//provider
|
||||
final infoAccountBalanceHomeProvider = StateNotifierProvider<
|
||||
InfoAccountBalanceHomeNotifier, InfoAccountBalanceHomeState>(
|
||||
(ref) => InfoAccountBalanceHomeNotifier(ref: ref),
|
||||
);
|
||||
@@ -0,0 +1,72 @@
|
||||
|
||||
import 'package:app_petty_cash/repository/home_repository.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../model/list_transaksi_model.dart';
|
||||
import '../../repository/transaksi_repository.dart';
|
||||
import '../../provider/dio_provider.dart';
|
||||
import '../../repository/base_repository.dart';
|
||||
|
||||
abstract class ListTransaksiHomeState extends Equatable {
|
||||
final DateTime date;
|
||||
const ListTransaksiHomeState(this.date);
|
||||
@override
|
||||
List<Object?> get props => [date];
|
||||
}
|
||||
|
||||
class ListTransaksiHomeStateInit extends ListTransaksiHomeState {
|
||||
ListTransaksiHomeStateInit() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class ListTransaksiHomeStateLoading extends ListTransaksiHomeState {
|
||||
ListTransaksiHomeStateLoading() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class ListTransaksiHomeStateError extends ListTransaksiHomeState {
|
||||
final String message;
|
||||
ListTransaksiHomeStateError({
|
||||
required this.message,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
class ListTransaksiHomeStateDone extends ListTransaksiHomeState {
|
||||
final List<ListTransaksiHomeModel> model;
|
||||
// final String resp;
|
||||
ListTransaksiHomeStateDone({
|
||||
required this.model,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
//notifier
|
||||
class ListTransaksiHomeNotifier extends StateNotifier<ListTransaksiHomeState> {
|
||||
final Ref ref;
|
||||
ListTransaksiHomeNotifier({
|
||||
required this.ref,
|
||||
}) : super(ListTransaksiHomeStateInit());
|
||||
|
||||
void listTransaksiHome(
|
||||
String companyid,
|
||||
) async {
|
||||
try {
|
||||
state = ListTransaksiHomeStateLoading();
|
||||
final dio = ref.read(dioProvider);
|
||||
final resp = await HomeRepository(dio: dio).getTransaksiListHome(
|
||||
companyid,
|
||||
);
|
||||
state = ListTransaksiHomeStateDone(model: resp);
|
||||
} catch (e) {
|
||||
if (e is BaseRepositoryException) {
|
||||
state = ListTransaksiHomeStateError(message: e.message.toString());
|
||||
} else {
|
||||
state = ListTransaksiHomeStateError(message: e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//provider
|
||||
final listTransaksiHomeProvider =
|
||||
StateNotifierProvider<ListTransaksiHomeNotifier, ListTransaksiHomeState>(
|
||||
(ref) => ListTransaksiHomeNotifier(ref: ref),
|
||||
);
|
||||
Reference in New Issue
Block a user