Last active
May 3, 2020 23:44
-
-
Save bjavier9/f4b7a96d89fc28453fb2aa7800b32886 to your computer and use it in GitHub Desktop.
Flutter task 2
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 'package:flutter/services.dart'; | |
import './widget/chart.dart'; | |
import 'dart:io'; | |
import 'model/transaction.dart'; | |
import 'widget/new_transacction.dart'; | |
import 'widget/transaction_list.dart'; | |
import 'package:flutter/cupertino.dart'; | |
void main() { | |
// WidgetsFlutterBinding.ensureInitialized(); | |
// SystemChrome.setPreferredOrientations( | |
// [ | |
// DeviceOrientation.portraitUp, | |
// DeviceOrientation.portraitDown | |
// ]); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Personal Expensive', | |
theme: ThemeData( | |
errorColor: Colors.red, | |
primaryColor: Colors.blue, | |
accentColor: Colors.blueAccent, | |
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(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
// String titleInput; | |
// String amountInput; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final List<Transaction> _userTransactions = [ | |
Transaction( | |
id: 't1', | |
title: 'New Shoes', | |
amount: 69.99, | |
date: DateTime.now(), | |
), | |
Transaction( | |
id: 't2', | |
title: 'Weekly Groceries', | |
amount: 16.53, | |
date: DateTime.now(), | |
), | |
]; | |
bool _showChart = false; | |
List<Transaction> get _recentTransactions { | |
return _userTransactions.where((tx) { | |
return tx.date.isAfter(DateTime.now().subtract(Duration(days: 7))); | |
}).toList(); | |
} | |
void _addNewTransaction( | |
String txTitle, double txAmount, DateTime chosenDate) { | |
final newTx = Transaction( | |
title: txTitle, | |
amount: txAmount, | |
date: chosenDate, | |
id: DateTime.now().toString(), | |
); | |
setState(() { | |
_userTransactions.add(newTx); | |
}); | |
} | |
void _startAddNewTransaction(BuildContext ctx) { | |
showModalBottomSheet( | |
context: ctx, | |
builder: (_) { | |
return GestureDetector( | |
onTap: () {}, | |
child: NewTransaction(addNewTrx: _addNewTransaction), | |
behavior: HitTestBehavior.opaque, | |
); | |
}, | |
); | |
} | |
void _deleteTransaction(String id) { | |
setState(() { | |
_userTransactions.removeWhere((tx) => tx.id == id); | |
}); | |
} | |
Widget _iosAppBar(){ | |
return CupertinoNavigationBar( | |
middle: const Text('Personal Expenses'), | |
trailing: Row( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
GestureDetector( | |
child: Icon(CupertinoIcons.add), | |
onTap: () => _startAddNewTransaction(context), | |
) | |
], | |
), | |
); | |
} | |
Widget _androidAppBar(){ | |
return AppBar( | |
title: const Text('Personal Expenses'), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.add), | |
onPressed: () => _startAddNewTransaction(context), | |
), | |
], | |
); | |
} | |
Widget _appbarLogic() { | |
return Platform.isIOS | |
?_iosAppBar() | |
: _androidAppBar(); | |
} | |
List<Widget> _buildLandscapeContent( | |
MediaQueryData mediaQuery, | |
AppBar appBar, | |
Widget txListWidget, | |
) { | |
return [ | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Show Chart', | |
style: Theme.of(context).textTheme.title, | |
), | |
Switch.adaptive( | |
activeColor: Theme.of(context).accentColor, | |
value: _showChart, | |
onChanged: (val) { | |
setState(() { | |
_showChart = val; | |
}); | |
}, | |
), | |
], | |
), | |
_showChart | |
? Container( | |
height: (mediaQuery.size.height - | |
appBar.preferredSize.height - | |
mediaQuery.padding.top) * | |
0.7, | |
child: Chart( | |
recentTransations: _recentTransactions, | |
), | |
) | |
: txListWidget | |
]; | |
} | |
List<Widget> _buildPortraitContent( | |
MediaQueryData mediaQuery, | |
AppBar appBar, | |
Widget txListWidget, | |
) { | |
return [ | |
Container( | |
height: (mediaQuery.size.height - | |
appBar.preferredSize.height - | |
mediaQuery.padding.top) * | |
0.3, | |
child: Chart( | |
recentTransations: _recentTransactions, | |
), | |
), | |
txListWidget | |
]; | |
} | |
@override | |
Widget build(BuildContext context) { | |
final mediaQuery = MediaQuery.of(context); | |
final isLandscape = mediaQuery.orientation == Orientation.landscape; | |
final PreferredSizeWidget appBar = _appbarLogic(); | |
final txtListWidget = Container( | |
height: (mediaQuery.size.height - appBar.preferredSize.height) * 0.7, | |
child: TransactionList( | |
transactions: _userTransactions, deleteTrans: _deleteTransaction)); | |
final pagbody = SafeArea( | |
child: SingleChildScrollView( | |
child: Column( | |
// mainAxisAlignment: MainAxisAlignment.start, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
if (isLandscape) | |
..._buildLandscapeContent( | |
mediaQuery, | |
appBar, | |
txtListWidget, | |
), | |
if (!isLandscape) | |
..._buildPortraitContent( | |
mediaQuery, | |
appBar, | |
txtListWidget, | |
), | |
// TransactionList(_userTransactions), | |
], | |
), | |
)); | |
return Platform.isIOS | |
? CupertinoPageScaffold( | |
child: pagbody, | |
navigationBar: appBar, | |
) | |
: Scaffold( | |
appBar: appBar, | |
body: _userTransactions.isEmpty | |
? LayoutBuilder(builder: (ctx, constraints) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'No transactions added yet!', | |
style: Theme.of(context).textTheme.title, | |
), | |
const SizedBox( | |
height: 10, | |
), | |
Container( | |
height: constraints.maxHeight * 0.6, | |
child: Image.asset( | |
'assets/img/box.png', | |
fit: BoxFit.cover, | |
)) | |
], | |
), | |
); | |
}) | |
: pagbody, | |
floatingActionButtonLocation: | |
FloatingActionButtonLocation.centerFloat, | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => _startAddNewTransaction(context), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment