import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/buyer_model.dart'; import 'package:fcs/model/language_model.dart'; import 'package:fcs/pages/util.dart'; import 'package:fcs/vo/buyer.dart'; import 'package:fcs/widget/localization/app_translations.dart'; import 'package:fcs/widget/progress.dart'; import '../fcs/common/theme.dart'; class QuotaPage extends StatefulWidget { final Buyer buyer; final bool isApproved; const QuotaPage({this.buyer, this.isApproved}); @override _QuotaPageState createState() => _QuotaPageState(); } class _QuotaPageState extends State { final formKey = GlobalKey(); TextEditingController _dailyQuota = new TextEditingController(); TextEditingController _maxQuota = new TextEditingController(); TextEditingController _companyName = new TextEditingController(); TextEditingController _accountName = new TextEditingController(); Buyer buyer = Buyer(); bool _isLoading = false; @override void initState() { super.initState(); if (widget.buyer != null) { this.buyer = widget.buyer; _companyName.text = buyer.bizName; _accountName.text = buyer.userName; _dailyQuota.text = buyer.dailyQuota.toString(); _maxQuota.text = buyer.maxQuota.toString(); Provider.of(context, listen: false) .loadBuyerProducts(buyer, force: true) .then((b) { setState(() { buyer = b; }); }); } } @override Widget build(BuildContext context) { var languageModel = Provider.of(context); final companyNameBox = Container( padding: EdgeInsets.only(top: 0, left: 20, right: 20), child: TextFormField( controller: _companyName, autofocus: false, cursorColor: primaryColor, readOnly: true, style: textStyle, decoration: new InputDecoration( labelText: AppTranslations.of(context).text("reg.biz_name"), labelStyle: languageModel.isEng ? labelStyle : labelStyleMM, border: InputBorder.none, focusedBorder: InputBorder.none, icon: Icon( Icons.business, color: primaryColor, ), ), )); final accountNameBox = Container( padding: EdgeInsets.only(top: 0, left: 20, right: 20), child: TextFormField( controller: _accountName, autofocus: false, cursorColor: primaryColor, readOnly: true, style: textStyle, decoration: new InputDecoration( labelText: AppTranslations.of(context).text("buyer.account_name"), labelStyle: languageModel.isEng ? labelStyle : labelStyleMM, border: InputBorder.none, focusedBorder: InputBorder.none, icon: Icon( Icons.business, color: primaryColor, ), ), )); final dailyQuotaBox = Container( padding: EdgeInsets.only(top: 0, left: 20, right: 15), child: TextFormField( controller: _dailyQuota, autofocus: false, cursorColor: primaryColor, style: textStyle, keyboardType: TextInputType.number, decoration: new InputDecoration( labelText: AppTranslations.of(context).text("reg.quota"), labelStyle: languageModel.isEng ? labelStyle : labelStyleMM, icon: Icon( Icons.business, color: primaryColor, ), enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), ), validator: _validateQuota, )); final maxQuotaBox = Container( padding: EdgeInsets.only(top: 0, left: 20, right: 15), child: TextFormField( controller: _maxQuota, autofocus: false, cursorColor: primaryColor, style: textStyle, keyboardType: TextInputType.number, decoration: new InputDecoration( labelText: AppTranslations.of(context).text("reg.max_quota"), labelStyle: languageModel.isEng ? labelStyle : labelStyleMM, icon: Icon( Icons.business, color: primaryColor, ), enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), ), validator: _validateQuota, )); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( backgroundColor: primaryColor, title: Text("Quota"), actions: [ IconButton( icon: Icon(Icons.send), onPressed: () { showConfirmDialog(context, "buyer.allocate.quota.confirm", () { _allocate(); }); }, ) ], ), body: Form( key: formKey, child: Container( padding: EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10), child: ListView( children: [ Container( height: 500, child: Card( elevation: 23, child: Column( children: [ accountNameBox, companyNameBox, dailyQuotaBox, maxQuotaBox, ], ), ), ) ], ), ), ), ), ); } String _validateQuota(value) { if (value.isEmpty) { return "Invalid number"; } return null; } _allocate() async { if (!formKey.currentState.validate()) { return; } setState(() { _isLoading = true; }); try { buyer.dailyQuota = int.parse(_dailyQuota.text); buyer.maxQuota = int.parse(_maxQuota.text); if (widget.isApproved) { await Provider.of(context).allocate(buyer); Navigator.pop(context, true); } else { Navigator.pop(context, this.buyer); } } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }