import 'package:flutter/material.dart';
void main() => runApp(ByteBankApp());
class ByteBankApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: TransferList(),
),
);
}
}
class TransferForm extends StatelessWidget {
final TextEditingController _controlCampNumberAccount =
TextEditingController();
final TextEditingController _controlValueCamp = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Criando Transferência'),
),
body: Column(
children: <Widget>[
Editor(
controller: _controlCampNumberAccount,
label: 'Número da Conta',
clew: '0000',
),
Editor(
controller: _controlValueCamp,
label: 'Valor',
clew: '0.00',
icon: Icons.monetization_on,
),
RaisedButton(
onPressed: () => _createTransfer(context),
child: Text('Confirmar'),
),
],
));
}
void _createTransfer(BuildContext context) {
final int numberAccount = int.tryParse(_controlCampNumberAccount.text);
final double value = double.tryParse(_controlValueCamp.text);
if (numberAccount != null && value != null) {
final createdTransfer = Transfer(value, numberAccount);
Navigator.pop(context, createdTransfer);
}
}
}
class TransferList extends StatefulWidget {
final List<Transfer> _transfers = List();
@override
State<StatefulWidget> createState() {
return TransferListState();
}
}
class TransferListState extends State<TransferList> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Transferências'),
),
body: ListView.builder(
itemCount: widget._transfers.length,
itemBuilder: (context, index) {
final transfer = widget._transfers[index];
return TransferItem(transfer);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
final Future<Transfer> future =
Navigator.push(context, MaterialPageRoute(builder: (context) {
return TransferForm();
}));
future.then((transferReceived) {
widget._transfers.add(transferReceived);
debugPrint('$transferReceived');
});
},
),
);
}
}
class TransferItem extends StatelessWidget {
final Transfer _transferencia;
TransferItem(this._transferencia);
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: Icon(Icons.monetization_on),
title: Text(_transferencia.value.toString()),
subtitle: Text(_transferencia.numberAccount.toString()),
));
}
}
class Editor extends StatelessWidget {
final TextEditingController controller;
final String label;
final String clew;
final IconData icon;
Editor({this.controller, this.label, this.clew, this.icon});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: controller,
style: TextStyle(fontSize: 24.0),
decoration: InputDecoration(
icon: icon != null ? Icon(icon) : null,
labelText: label,
hintText: clew,
),
keyboardType: TextInputType.number,
),
);
}
}
class Transfer {
final double value;
final int numberAccount;
Transfer(this.value, this.numberAccount);
@override
String toString() {
return 'Transfer{value: $value, numberAccount: $numberAccount}';
}
}