Created
March 22, 2020 22:27
-
-
Save gabrielbmoro/20b7aa052ecc88e8e6ddfda77c8a7d15 to your computer and use it in GitHub Desktop.
Learning Flutter - Alura - First course
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class Editor extends StatelessWidget { | |
final TextEditingController controller; | |
final String labelText; | |
final String hintText; | |
final IconData icon; | |
Editor(this.labelText, {this.controller, this.hintText, this.icon}); | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: TextField( | |
controller: this.controller, | |
style: TextStyle( | |
fontSize: 24.0, | |
), | |
decoration: InputDecoration( | |
labelText: this.labelText, | |
hintText: this.hintText, | |
icon: icon != null ? Icon(icon) : null, | |
), | |
keyboardType: TextInputType.number, | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutterapptesting/components/editor_component.dart'; | |
import 'package:flutterapptesting/models/transaction_model.dart'; | |
const _appBarTitle = 'Criando transferência'; | |
const _buttonLabel = 'Confirmar'; | |
const _accountNumberText = 'Número da Conta'; | |
const _accountNumberHint = '0000'; | |
const _valueText = 'Valor'; | |
const _valueHint = '0.0'; | |
class TransactionForm extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return TransactionFormState(); | |
} | |
} | |
class TransactionFormState extends State<TransactionForm> { | |
final TextEditingController _accountNumberFieldController = | |
TextEditingController(); | |
final TextEditingController _valueFieldController = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text( | |
_appBarTitle, | |
), | |
), | |
body: SingleChildScrollView( | |
child: Column( | |
children: <Widget>[ | |
Editor( | |
_accountNumberText, | |
controller: _accountNumberFieldController, | |
hintText: _accountNumberHint, | |
), | |
Editor( | |
_valueText, | |
controller: _valueFieldController, | |
hintText: _valueHint, | |
icon: Icons.monetization_on, | |
), | |
RaisedButton( | |
child: Text(_buttonLabel), | |
onPressed: () => _createTransaction(context), | |
) | |
], | |
), | |
), | |
); | |
} | |
void _createTransaction(context) { | |
final int accountNumber = int.tryParse(_accountNumberFieldController.text); | |
final double value = double.tryParse(_valueFieldController.text); | |
if (value != null && accountNumber != null) { | |
Navigator.pop( | |
context, | |
Transaction(value, accountNumber), | |
); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutterapptesting/models/transaction_model.dart'; | |
import 'package:flutterapptesting/screens/transaction/form.dart'; | |
const _appBarTitle = 'Transferências'; | |
class TransactionList extends StatefulWidget { | |
final List<Transaction> _transactions = List(); | |
@override | |
State<StatefulWidget> createState() { | |
return TransactionListState(); | |
} | |
} | |
class TransactionListState extends State<TransactionList> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(_appBarTitle), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) { | |
return TransactionForm(); | |
}, | |
), | |
).then( | |
(transactionReceived) => _updateList(transactionReceived), | |
); | |
}, | |
), | |
body: ListView.builder( | |
itemCount: widget._transactions.length, | |
itemBuilder: (context, index) { | |
final transaction = widget._transactions[index]; | |
return TransactionItem(transaction); | |
}, | |
), | |
); | |
} | |
void _updateList(Transaction t) { | |
if (t != null) { | |
setState(() { | |
widget._transactions.add(t); | |
}); | |
} | |
} | |
} | |
class TransactionItem extends StatelessWidget { | |
final Transaction _transaction; | |
TransactionItem(this._transaction); | |
@override | |
Widget build(BuildContext context) { | |
return Card( | |
child: ListTile( | |
leading: Icon(Icons.monetization_on), | |
title: Text(_transaction.value.toString()), | |
subtitle: Text(_transaction.accountNumber.toString()), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutterapptesting/screens/transaction/list.dart'; | |
void main() => runApp(ByteBankApp()); | |
class ByteBankApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
primaryColor: Colors.green[900], | |
accentColor: Colors.blueAccent[700], | |
buttonTheme: ButtonThemeData( | |
buttonColor: Colors.blueAccent[700], | |
textTheme: ButtonTextTheme.primary, | |
), | |
), | |
home: TransactionList(), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transaction { | |
final double value; | |
final int accountNumber; | |
Transaction( | |
this.value, | |
this.accountNumber, | |
); | |
@override | |
String toString() { | |
return "valor $value ____ numeroConta $accountNumber"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment