import 'package:flutter/material.dart';
void main() => runApp(ByteBankapp());
class ByteBankapp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListaTransferencias(),
),
);
}
}
class ListaTransferencias extends StatefulWidget {
final List<Transferencia> _transferencias = [];
@override
State<StatefulWidget> createState() {
return ListaTransferenciaState();
}
}
class ListaTransferenciaState extends State<ListaTransferencias>{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Transferências')),
body: ListView.builder(
itemCount: widget._transferencias.length,
itemBuilder: (context, indice) {
final transferencia = widget._transferencias[indice];
return ItemTransferencia(transferencia);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
final Future<Transferencia?> future = Navigator.push(context, MaterialPageRoute(builder: (context) {
return FormularioTransferencia();
}));
future.then((transferenciaRecebida) {
debugPrint('Chegou no then do Future');
debugPrint('$transferenciaRecebida');
widget._transferencias.add(transferenciaRecebida!);
});
},
child: Icon(Icons.add),
));
}
}
class ItemTransferencia extends StatelessWidget {
final Transferencia _transferencia;
ItemTransferencia(this._transferencia);
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: Icon(
Icons.monetization_on,
),
title: Text(_transferencia.valor.toString()),
subtitle: Text(_transferencia.numeroConta.toString()),
),
);
}
}
class FormularioTransferencia extends StatelessWidget {
final TextEditingController _controladorCampoValor = TextEditingController();
final TextEditingController _controladorCampoNumeroConta =
TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Criando Transferência'),
),
body: Column(
children: <Widget>[
Editor(
controlador: _controladorCampoNumeroConta,
rotulo: 'Número da Conta',
dica: '0000',
icone: Icons.account_balance,
),
Editor(
controlador: _controladorCampoValor,
rotulo: 'Valor',
dica: '0.00',
icone: Icons.monetization_on),
ElevatedButton(
onPressed: () => _criaTransferencia(context),
child: Text('Confirmar'),
),
],
));
}
void _criaTransferencia(BuildContext context) {
final int? numeroConta =
int.tryParse(_controladorCampoNumeroConta.text);
final double? valorTransferencia =
double.tryParse(_controladorCampoValor.text);
if (numeroConta != null && valorTransferencia != null) {
final transferenciaCriada =
Transferencia(valorTransferencia, numeroConta);
debugPrint('Criando Transferência');
debugPrint('$transferenciaCriada');
Navigator.pop(context, transferenciaCriada);
}
}
}
class Editor extends StatelessWidget {
final TextEditingController controlador;
final String rotulo;
final String dica;
final IconData icone;
Editor(
{required this.controlador,
required this.rotulo,
required this.dica,
required this.icone});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: controlador,
style: TextStyle(fontSize: 24),
decoration: InputDecoration(
icon: Icon(icone),
labelText: rotulo,
hintText: dica,
),
keyboardType: TextInputType.number,
),
);
}
}
class Transferencia {
final double valor;
final int numeroConta;
Transferencia(this.valor, this.numeroConta);
@override
String toString() {
return 'Transferencia{valor: $valor, numeroConta: $numeroConta}';
}
}