Created
October 19, 2019 09:59
-
-
Save shikto1/9c6d51948e10b3adfa4401aef4910a06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:oyobill/base/BaseState.dart'; | |
import 'package:oyobill/data/network/APIs.dart'; | |
import 'package:oyobill/data/network/api_response/TopupHistoryResponse.dart'; | |
import 'package:oyobill/ui/history/topup_history/topup_history_contract.dart'; | |
import 'package:intl/intl.dart'; | |
class TopupHistory extends StatefulWidget { | |
static const route = "/topup-history"; | |
@override | |
_TopupHistoryState createState() => new _TopupHistoryState(); | |
} | |
class _TopupHistoryState extends BaseState<TopupHistory> | |
implements TopupHistoryView { | |
// Pagination | |
List<SingleTopup> _hisotories = []; | |
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = GlobalKey< | |
RefreshIndicatorState>(); | |
ScrollController _scrollController = ScrollController(); | |
String _nextPageUrl; | |
bool _isFirstTimeLoading = false; | |
bool _isPaginating = false; | |
TopupHistoryPresenterImpl _presenter; | |
@override | |
void initState() { | |
super.initState(); | |
_scrollController.addListener(() { | |
if (_scrollController.position.pixels == | |
_scrollController.position.maxScrollExtent) { | |
if (_nextPageUrl != null && _isPaginating == false) { | |
loadMoreData(); | |
} | |
} | |
}); | |
_presenter = TopupHistoryPresenterImpl(this); | |
_isFirstTimeLoading = true; | |
_presenter.getTopUpHistory(APIs.TOPUP_HISTORY); | |
} | |
@override | |
void dispose() { | |
_scrollController.dispose(); | |
super.dispose(); | |
} | |
Widget _buildProgressIndicator() { | |
return Padding( | |
padding: EdgeInsets.all(8.0), | |
child: Center( | |
child: Opacity(opacity: _isPaginating ? 1.0 : 00, | |
child: CircularProgressIndicator()), | |
), | |
); | |
} | |
Widget _buildItemView(SingleTopup singleTopUp) { | |
return Column( | |
children: <Widget>[ | |
Container( | |
padding: EdgeInsets.symmetric(vertical: 14, horizontal: 8), | |
child: Row( | |
children: <Widget>[ | |
Expanded( | |
child: Center( | |
child: Column( | |
children: <Widget>[ | |
Text(DateFormat("dd MMM, yyyy").format( | |
DateFormat("yyyy-MM-dd HH:mm:ss").parse( | |
singleTopUp.createdAt)), | |
style: TextStyle(fontSize: 12.0, | |
color: Colors.black, | |
fontWeight: FontWeight.w500)), | |
Text(DateFormat("hh:mm:ss a").format( | |
DateFormat("yyyy-MM-dd HH:mm:ss").parse( | |
singleTopUp.createdAt)), | |
style: TextStyle(fontSize: 10.0)), | |
], | |
) | |
) | |
), | |
Expanded( | |
child: Center( | |
child: Column( | |
children: <Widget>[ | |
Text(singleTopUp.number.replaceFirst( | |
"88", ""), | |
style: TextStyle(fontSize: 10.0)), | |
Text(singleTopUp.type == 0 | |
? "Prepaid" | |
: "Postpaid", | |
style: TextStyle(fontSize: 10.0)) | |
], | |
), | |
) | |
), | |
Expanded( | |
child: Center( | |
child: Text("${singleTopUp.amount} TK", | |
style: TextStyle(fontSize: 12.0, | |
color: Colors.black, | |
fontWeight: FontWeight.w400)), | |
) | |
), Expanded( | |
child: Center( | |
child: Column( | |
children: <Widget>[ | |
Text(singleTopUp.status == "1000" | |
? "SUCCESS" | |
: "FAILED", style: TextStyle(fontSize: 10.0)), | |
Text("${singleTopUp.trxnId}", | |
style: TextStyle(fontSize: 7.0)) | |
], | |
), | |
), | |
) | |
], | |
), | |
), | |
Divider( | |
height: 5.0 | |
) | |
], | |
); | |
} | |
Widget _buildList() { | |
return RefreshIndicator( | |
onRefresh: () async { | |
// On Refresh....... | |
}, | |
key: _refreshIndicatorKey, | |
child: ListView.builder( | |
controller: _scrollController, | |
itemCount: _hisotories.length + 1, // +1 For ProgressBar | |
physics: const AlwaysScrollableScrollPhysics(), | |
itemBuilder: (BuildContext context, int index) { | |
if (index == _hisotories.length) { | |
if (_nextPageUrl != null) | |
return _buildProgressIndicator(); | |
} else { | |
return _buildItemView(_hisotories[index]); | |
} | |
}, | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
buildContext = context; | |
return Scaffold( | |
key: scaffoldKey, | |
appBar: AppBar( | |
title: Text('Topup History'), | |
), | |
body: _isFirstTimeLoading | |
? Center( | |
child: CircularProgressIndicator(), | |
) | |
: _buildList(), | |
resizeToAvoidBottomPadding: false, | |
); | |
} | |
@override | |
void loadMoreData() { | |
if (!_isPaginating) { | |
setState(() { | |
_isPaginating = true; | |
}); | |
} | |
_presenter.getTopUpHistory(_nextPageUrl); | |
} | |
@override | |
void topUpHistoryDidReceived(TopupHistoryData historyData) { | |
this._nextPageUrl = historyData.nextPageUrl; | |
if (_isPaginating) { | |
setState(() { | |
_isPaginating = false; | |
_hisotories.addAll(historyData.data); | |
}); | |
} else { | |
setState(() { | |
_isFirstTimeLoading = false; | |
_hisotories = historyData.data; | |
}); | |
} | |
} | |
@override | |
void onNetworkCallStarted(String loadingMsg) { | |
} | |
@override | |
void onNetworkCallEnded() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment