import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/language_model.dart'; import 'package:fcs/model/product_model.dart'; import 'package:fcs/pages/util.dart'; import 'package:fcs/vo/product.dart'; import 'package:fcs/widget/localization/app_translations.dart'; import 'package:fcs/widget/progress.dart'; import '../fcs/common/theme.dart'; class ProductEditItem extends StatefulWidget { final Product product; const ProductEditItem({Key key, this.product}) : super(key: key); @override _ProductEditItemState createState() => _ProductEditItemState(); } class _ProductEditItemState extends State { TextEditingController nameController = new TextEditingController(); TextEditingController priceController = new TextEditingController(); TextEditingController orderController = new TextEditingController(); int color = primaryColor.value; bool isDisable = false; final _formKey = GlobalKey(); bool _isLoading = false; @override void initState() { super.initState(); if (widget.product != null) { this.color = widget.product.color; nameController.text = widget.product.name; priceController.text = widget.product.price.toString(); orderController.text = widget.product.displayOrder.toString(); if (widget.product.isDisable != null) { isDisable = widget.product.isDisable; } else { isDisable = false; } } } @override Widget build(BuildContext context) { var maingModel = Provider.of(context); final nameWidget = Container( padding: EdgeInsets.only(top: 10), child: TextFormField( controller: nameController, autofocus: true, cursorColor: primaryColor, style: textStyle, decoration: new InputDecoration( icon: InkWell( child: Icon( FontAwesomeIcons.tag, color: Color(this.color), size: 25, ), onTap: () => showColorPicker(context, Color(this.color), (color) { setState(() { this.color = color.value; }); }), ), labelText: AppTranslations.of(context).text("product.name"), labelStyle: maingModel.isEng ? labelStyle : labelStyleMM, enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), ), validator: (value) { if (value.isEmpty) { return AppTranslations.of(context).text("product.name_empty"); } return null; }, ), ); final priceInput = Container( child: TextFormField( controller: priceController, style: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal), keyboardType: TextInputType.number, cursorColor: primaryColor, decoration: new InputDecoration( icon: Icon( FontAwesomeIcons.moneyBill, color: primaryColor, size: 25, ), border: InputBorder.none, labelText: AppTranslations.of(context).text("product.new_price"), labelStyle: maingModel.isEng ? labelStyle : labelStyleMM, enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), ), validator: (value) { if (value.isEmpty) { return AppTranslations.of(context).text("product.price_empty"); } return null; }, ), ); final orderInput = Container( child: TextFormField( controller: orderController, style: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal), keyboardType: TextInputType.number, cursorColor: primaryColor, decoration: new InputDecoration( icon: Icon( Icons.sort, color: primaryColor, size: 25, ), border: InputBorder.none, labelText: AppTranslations.of(context).text("product.order"), labelStyle: maingModel.isEng ? labelStyle : labelStyleMM, enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), ), validator: (value) { if (value.isEmpty) { return AppTranslations.of(context).text("product.order_empty"); } return null; }, ), ); final disableBox = Container( padding: EdgeInsets.only(top: 10), child: CheckboxListTile( title: Text("Disable"), value: isDisable, activeColor: primaryColor, onChanged: (value) { setState(() { isDisable = value; }); }, controlAffinity: ListTileControlAffinity.leading, )); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( title: Text(AppTranslations.of(context).text("product.item"), style: maingModel.isEng ? TextStyle(color: Colors.white, fontSize: 20.0) : TextStyle( color: Colors.white, fontSize: 20.0, fontFamily: 'MyanmarUnicode')), actions: [ IconButton( icon: Icon(Icons.save), onPressed: () { if (!_formKey.currentState.validate()) return; Provider.of(context, listen: false).saveProduct( widget.product, nameController.text, priceController.text, orderController.text, color, isDisable); Product _product = new Product(); if (widget.product != null) { _product = widget.product; _product.name = nameController.text; _product.price = int.parse(priceController.text); _product.color = color; _product.displayOrder = int.parse(orderController.text); _product.isDisable = isDisable; if (_product.id == null) { _product.action = "create"; } else { _product.action = "update"; } } Navigator.pop(context, _product); }, ) ], backgroundColor: primaryColor, ), body: Form( key: _formKey, child: SingleChildScrollView( padding: EdgeInsets.only( left: 25.0, right: 25.0, ), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ nameWidget, priceInput, orderInput, this.widget.product != null ? disableBox : Container(), ], ), ), ), ), ); } }