Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:coocafe_tecnico_app/helpers/produtor_helper.dart';
- import 'package:coocafe_tecnico_app/widgets/customShowDialog.dart';
- import 'package:flutter/material.dart';
- class DadosProdutorPage extends StatefulWidget {
- final Produtor produtor;
- DadosProdutorPage({this.produtor});
- @override
- _DadosProdutorPageState createState() => _DadosProdutorPageState();
- }
- class _DadosProdutorPageState extends State<DadosProdutorPage> {
- Produtor _produtor = Produtor();
- ProdutorHelper prodHelper = ProdutorHelper();
- final _nomeCtrl = TextEditingController();
- final _enderecoCtrl = TextEditingController();
- final _complCtrl = TextEditingController();
- final _bairroCtrl = TextEditingController();
- final _cidadeCtrl = TextEditingController();
- final _ufCtrl = TextEditingController();
- final _cepCtrl = TextEditingController();
- final _telCtrl = TextEditingController();
- final _celCtrl = TextEditingController();
- String telefone = "";
- String celular = "";
- bool _produtorEdited;
- @override
- void initState() {
- _produtorEdited = false;
- _produtor = widget.produtor;
- _nomeCtrl.text = _produtor.nome;
- _enderecoCtrl.text = _produtor.endereco;
- _complCtrl.text = _produtor.complemento;
- _bairroCtrl.text = _produtor.bairro;
- _cidadeCtrl.text = _produtor.cidade;
- _ufCtrl.text = _produtor.uf;
- _cepCtrl.text = _produtor.cep;
- _telCtrl.text = _produtor.telefone;
- _celCtrl.text = _produtor.celular;
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: _requestPop,
- child: Scaffold(
- appBar: AppBar(
- title: Text("Alterar Produtor"),
- centerTitle: true,
- ),
- body: Column(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- _createFormProdutor(context),
- ],
- ),
- ),
- );
- }
- Widget _createFormProdutor(BuildContext context) {
- return Expanded(
- child: ListView(
- padding: EdgeInsets.all(10.0),
- children: <Widget>[
- TextField(
- enabled: false,
- controller: _nomeCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "Nome",
- ),
- ),
- TextField(
- enabled: false,
- controller: _enderecoCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "Endereço",
- ),
- ),
- TextField(
- enabled: false,
- controller: _complCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "Complemento",
- ),
- ),
- TextField(
- enabled: false,
- controller: _bairroCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "Bairro",
- ),
- ),
- TextField(
- enabled: false,
- controller: _cidadeCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "Cidade",
- ),
- ),
- TextField(
- enabled: false,
- controller: _ufCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "UF",
- ),
- ),
- TextField(
- enabled: false,
- controller: _cepCtrl,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: "CEP",
- ),
- ),
- TextField(
- controller: _telCtrl,
- keyboardType: TextInputType.number,
- decoration: InputDecoration(
- labelText: "Telefone",
- ),
- onChanged: (value) {
- setState(() {
- _produtorEdited = true;
- _produtor.telefone = value;
- });
- },
- ),
- TextField(
- controller: _celCtrl,
- keyboardType: TextInputType.number,
- decoration: InputDecoration(
- labelText: "Celular",
- ),
- onChanged: (value) {
- setState(() {
- _produtorEdited = true;
- _produtor.celular = value;
- });
- },
- ),
- Padding(
- padding: EdgeInsets.only(top: 10.0),
- child: SizedBox(
- height: 44.0,
- child: RaisedButton(
- color: Colors.blue,
- textColor: Colors.white,
- child: Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Icon(Icons.save),
- Text(
- "Salvar",
- style: TextStyle(fontSize: 18.0),
- )
- ],
- ),
- ),
- onPressed: () {
- _produtor.situacao = "A";
- alterarProdutor(context, _produtor);
- },
- ),
- ),
- )
- ],
- ),
- );
- }
- void alterarProdutor(BuildContext context, Produtor produtor) {
- try{
- prodHelper.update(produtor).then((data) {
- if(data > 0) {
- showDialog(
- barrierDismissible: false,
- context: context,
- builder: (BuildContext context) {
- return CustomAlertDialog(
- title: Text("Sucesso"),
- content: Text("Produtor alterado com sucesso."),
- actions: <Widget>[
- FlatButton(
- child: Text("OK"),
- onPressed: () {
- Navigator.of(context).pop();
- Navigator.of(context).pop();
- },
- )
- ],
- );
- }
- );
- } else {
- showDialog(
- barrierDismissible: false,
- context: context,
- builder: (BuildContext context) {
- return CustomAlertDialog(
- title: Text("Atenção"),
- content: Text("Ocorreu um erro ao atualizar o produtor."),
- actions: <Widget>[
- FlatButton(
- child: Text("OK"),
- onPressed: () {
- Navigator.of(context).pop();
- },
- )
- ],
- );
- }
- );
- }
- });
- } catch(e) {
- _showDialog(context, e);
- }
- }
- Future<bool> _requestPop() {
- if (_produtorEdited) {
- showDialog(
- context: context,
- barrierDismissible: false,
- builder: (context) {
- return CustomAlertDialog(
- title: Text("Descartar Alterações?"),
- content: Text("Ao sair as alterações serão perdidas."),
- actions: <Widget>[
- FlatButton(
- child: Text("Sim"),
- onPressed: () {
- Navigator.pop(context);
- Navigator.pop(context);
- },
- ),
- FlatButton(
- child: Text("Não"),
- onPressed: () {
- Navigator.pop(context);
- },
- ),
- ],
- );
- }
- );
- return Future.value(false);
- } else {
- return Future.value(true);
- }
- }
- void _showDialog(BuildContext context, String texto) {
- // flutter defined function
- showDialog(
- barrierDismissible: false,
- context: context,
- builder: (BuildContext context) {
- // return object of type Dialog
- return CustomAlertDialog(
- title: new Text("Atenção"),
- content: new Text(texto),
- actions: <Widget>[
- // usually buttons at the bottom of the dialog
- new FlatButton(
- child: new Text("OK"),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ],
- );
- },
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement