Created
July 20, 2019 13:36
-
-
Save nglauber/3afbd8105844e9ef248630cdbe8db4a0 to your computer and use it in GitHub Desktop.
This file contains 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 './models/transaction.dart'; | |
import './widgets/chart.dart'; | |
import './widgets/transaction_list.dart'; | |
import './widgets/new_transaction.dart'; | |
void main() { | |
// Import this -> import 'package:flutter/services.dart'; | |
// SystemChrome.setPreferredOrientations([ | |
// DeviceOrientation.portraitUp, | |
// DeviceOrientation.portraitDown | |
// ]); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Personal Expenses', | |
theme: ThemeData( | |
primarySwatch: Colors.green, | |
accentColor: Colors.green, | |
errorColor: Colors.red, | |
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, | |
)), | |
), | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final List<Transaction> _userTransactions = []; | |
List<Transaction> get _recentTransactions { | |
return _userTransactions.where((tx) { | |
return tx.dateTime.isAfter(DateTime.now().subtract(Duration(days: 7))); | |
}).toList(); | |
} | |
bool _showChart = false; | |
_addTransaction(String title, double amount, DateTime date) { | |
final newTx = Transaction( | |
id: DateTime.now().toString(), | |
title: title, | |
amount: amount, | |
dateTime: date, | |
); | |
setState(() { | |
_userTransactions.add(newTx); | |
}); | |
} | |
_startAddNewTransaction(BuildContext ctx) { | |
showModalBottomSheet( | |
context: ctx, | |
builder: (bCtx) { | |
return NewTransaction(this._addTransaction); | |
}); | |
} | |
_deleteTransaction(id) { | |
setState(() { | |
_userTransactions.removeWhere((tx) => tx.id == id); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final isLandscape = MediaQuery.of(context).orientation == Orientation.landscape; | |
final isPortrait = MediaQuery.of(context).orientation == Orientation.portrait; | |
final appBar = AppBar( | |
title: Text('Personal Expenses'), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon( | |
Icons.add, | |
), | |
onPressed: () => _startAddNewTransaction(context), | |
) | |
], | |
); | |
final transactionList = Container( | |
child: TransactionList( | |
_userTransactions, this._deleteTransaction), | |
height: (MediaQuery.of(context).size.height - | |
appBar.preferredSize.height - | |
MediaQuery.of(context).padding.top) * | |
0.7, | |
); | |
final transactionChart = Container( | |
child: Chart(this._recentTransactions), | |
height: (MediaQuery.of(context).size.height - | |
appBar.preferredSize.height - | |
MediaQuery.of(context).padding.top) * | |
(isPortrait ? 0.3 : 0.7),); | |
return Scaffold( | |
appBar: appBar, | |
body: SingleChildScrollView( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
if (isLandscape) Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Show chart'), | |
Switch( | |
value: _showChart, | |
onChanged: (value) { | |
this.setState(() { | |
_showChart = value; | |
}); | |
}), | |
], | |
), | |
if (isPortrait) ...[transactionChart, transactionList], | |
if (isLandscape) _showChart ? transactionChart : transactionList, | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => _startAddNewTransaction(context), | |
), | |
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment