Skip to content

Commit

Permalink
Working app
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Hernandez committed Apr 26, 2020
1 parent 552451c commit 772fdee
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 83 deletions.
Binary file added assets/fonts/OpenSans-Bold.ttf
Binary file not shown.
Binary file added assets/fonts/OpenSans-Regular.ttf
Binary file not shown.
Binary file added assets/fonts/Quicksand-Bold.ttf
Binary file not shown.
Binary file added assets/fonts/Quicksand-Light.ttf
Binary file not shown.
Binary file added assets/fonts/Quicksand-Medium.ttf
Binary file not shown.
Binary file added assets/fonts/Quicksand-Regular.ttf
Binary file not shown.
Binary file added assets/images/waiting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
157 changes: 83 additions & 74 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,111 +1,120 @@
import 'package:flutter/material.dart';

import './models/transaction.dart';
import './widgets/new_transaction.dart';
import './widgets/transaction_list.dart';
import './widgets/chart.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
title: 'Personal Expenses',
home: MyHomePage(),
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
primarySwatch: Colors.purple,
accentColor: Colors.amber,

fontFamily: 'Quicksand',
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(fontFamily: 'OpenSans', fontSize: 18, fontWeight: FontWeight.bold),
button: TextStyle(color: Colors.white),
),
appBarTheme: AppBarTheme(
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(fontFamily: 'OpenSans', fontSize: 20, fontWeight: FontWeight.bold)
)
)
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
debugShowCheckedModeBanner: false,

);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
final List<Transaction> _userTransactions = [
// Transaction(id: '1', title: 'New Shoe', amount: 60, date: DateTime.now()),
// Transaction(
// id: '2',
// title: 'Weekly groceries',
// amount: 54.99,
// date: DateTime.now()),
// Transaction(
// id: '3', title: 'Phone bill', amount: 19.99, date: DateTime.now()),
// Transaction(
// id: '4', title: 'Internet', amount: 29.99, date: DateTime.now()),
];

List<Transaction> get _recentTransactions {
return _userTransactions.where((tx){
return tx.date.isAfter(DateTime.now().subtract(Duration(days:7)));
}).toList();
}

void _addNewTransaction(String title, double amount, DateTime chosenDate){
final newTx = Transaction(
title: title,
amount: amount,
date: chosenDate,
id: DateTime.now().toString(),
);
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
_userTransactions.add(newTx);
});
}

void _deleteTransaction(String id){
setState(() {
_userTransactions.removeWhere((tx) => tx.id == id);
});

}

void _startAddNewTransaction(BuildContext ctx){
showModalBottomSheet(context: ctx, builder: (_){
return NewTransaction(_addNewTransaction);
});
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
title: Text(
'Personal Expenses',
style: TextStyle(
fontFamily: 'OpenSans'
),
),
actions: <Widget>[
IconButton(icon: Icon(Icons.add,), onPressed:()=>_startAddNewTransaction(context),),
],
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Chart(_recentTransactions),
TransactionList(_userTransactions, _deleteTransaction),
],
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
onPressed: ()=>_startAddNewTransaction(context),
),
);
}
}
16 changes: 16 additions & 0 deletions lib/models/transaction.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/foundation.dart';

class Transaction{
final String id;
final String title;
final double amount;
final DateTime date;

Transaction({
@required this.id,
@required this.title,
@required this.amount,
@required this.date
});

}
64 changes: 64 additions & 0 deletions lib/widgets/chart.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import '../models/transaction.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import './chart_bar.dart';

class Chart extends StatelessWidget {
final List<Transaction> recentTransactions;

Chart(this.recentTransactions);

List<Map<String, Object>> get groupedTransactionValues {
return List.generate(7, (index) {
final weekday = DateTime.now().subtract(
Duration(days: index),
);
var totalSum = 0.0;

for (var i = 0; i < recentTransactions.length; i++) {
if (recentTransactions[i].date.day == weekday.day &&
recentTransactions[i].date.month == weekday.month &&
recentTransactions[i].date.year == weekday.year) {
totalSum += recentTransactions[i].amount;
}
}

return {
'day': DateFormat.E().format(weekday).substring(0, 1),
'amount': totalSum
};
}).reversed.toList();
}

double get totalSpending {
return groupedTransactionValues.fold(0.0, (sum, item) {
return sum + item['amount'];
});
}

@override
Widget build(BuildContext context) {
print(groupedTransactionValues);
return Card(
elevation: 6,
margin: EdgeInsets.all(20),
child: Container(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: groupedTransactionValues.map((tx) {
print(totalSpending == 0.0 ? 0.0 : (tx['amount'] as double) / totalSpending);
return Flexible(
fit: FlexFit.tight,
child: ChartBar(
tx['day'],
tx['amount'],
totalSpending == 0.0 ? 0.0 : (tx['amount'] as double) / totalSpending,
),
);
}).toList(),
),
),
);
}
}
54 changes: 54 additions & 0 deletions lib/widgets/chart_bar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';

class ChartBar extends StatelessWidget {
final String label;
final double spendingAmount;
final double spendingPctOfTotal;

ChartBar(this.label, this.spendingAmount, this.spendingPctOfTotal);

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
height: 20,
child: FittedBox(
child: Text('\$${spendingAmount.toStringAsFixed(0)}'),
),
),
SizedBox(
height: 4,
),
Container(
height: 60,
width: 10,
child: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1.0),
color: Color.fromRGBO(220, 220, 220, 1),
borderRadius: BorderRadius.circular(10),
),
),
FractionallySizedBox(
heightFactor: spendingPctOfTotal,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
SizedBox(
height: 4,
),
Text(label)
],
);
}
}
Loading

0 comments on commit 772fdee

Please sign in to comment.