Advertisement
Guest User

Stack Listviews

a guest
Aug 18th, 2018
1,410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.69 KB | None | 0 0
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:cryptick/data/crypto_data.dart';
  4. import 'package:cryptick/modules/crypto_presenter.dart';
  5. import 'package:shimmer/shimmer.dart';
  6. import 'dart:async';
  7. import 'package:path_provider/path_provider.dart';
  8. import 'dart:io';
  9.  
  10.  
  11.  
  12. class HomePage extends StatefulWidget {
  13. @override
  14. _HomePageState createState() => new _HomePageState();
  15. }
  16.  
  17. class MoreInfoScreen extends StatelessWidget {
  18. @override
  19. Widget build(BuildContext context) {
  20. return new Scaffold(
  21. appBar: new AppBar(
  22. centerTitle: true,
  23. backgroundColor: Colors.black,
  24. title: new Text("More Info",
  25. style: new TextStyle(
  26. fontFamily: 'PlayfairDisplay',
  27. letterSpacing: 0.8
  28. )
  29. ),
  30. ),
  31. body: new Center(
  32. child: new Card(
  33. child: new Column(
  34. children: [
  35. new ListTile(
  36. title: new Text('Squared Software London',
  37. style: new TextStyle(fontWeight: FontWeight.w500)),
  38. subtitle: new Text('Cryp-Tick Exchange'),
  39. leading: new CircleAvatar(
  40. radius: 18.5,
  41. backgroundImage: new NetworkImage('https://instagram.flhr4-2.fna.fbcdn.net/vp/d5dfc743e547f88c553af0b9bdc33ece/5BCDB00D/t51.2885-19/s150x150/34885182_217909492157471_8233391730213257216_n.jpg'),
  42. )
  43. ),
  44. new ListTile(
  45. subtitle: new Text('Contact us at: squaredsoftwarelondon@gmail.com',
  46. style: new TextStyle(fontWeight: FontWeight.w500)),
  47. leading: new Icon(
  48. Icons.alternate_email,
  49. color: Colors.red[500],
  50. ),
  51. ),
  52. new Divider(),
  53. new ListTile(
  54. title: new Text('Cryp-Tick Exchange uses the Public Ticker V1 API available from: https://coinmarketcap.com/api/',
  55. style: new TextStyle(fontWeight: FontWeight.w500)),
  56. ),
  57. ],
  58. ),
  59. ),
  60. ),
  61. );
  62.  
  63. }
  64. }
  65.  
  66.  
  67. class SettingsScreen extends StatelessWidget {
  68. @override
  69. Widget build(BuildContext context) {
  70. return new Scaffold(
  71. appBar: new AppBar(
  72. centerTitle: true,
  73. backgroundColor: Colors.black,
  74. title: new Text("Settings",
  75. style: new TextStyle(
  76. fontFamily: 'PlayfairDisplay',
  77. letterSpacing: 0.8
  78. )
  79. ),
  80. ),
  81. body: new Center(
  82. child: new Column(
  83. children: [
  84. new ListTile(
  85. title: new MaterialButton(
  86. child: new Text(
  87. 'Fiat Currencies',
  88. textAlign: TextAlign.center,
  89. style: new TextStyle(
  90. fontSize: 20.0,
  91. letterSpacing: 0.8,
  92. fontFamily: 'PlayfairDisplay'
  93. ),
  94. ),
  95. onPressed: (){
  96. Navigator.push(
  97. context,
  98. new MaterialPageRoute(builder: (context) => new CurrencyScreen()),
  99. );
  100. }
  101. ),
  102. ),
  103. new Divider(),
  104. new ListTile(
  105. title: new MaterialButton(
  106. child: new Text(
  107. 'Fill with more options',
  108. textAlign: TextAlign.center,
  109. style: new TextStyle(
  110. fontSize: 20.0,
  111. letterSpacing: 0.8,
  112. fontFamily: 'PlayfairDisplay'
  113. ),
  114. ),
  115. onPressed: (){
  116. Navigator.push(
  117. context,
  118. new MaterialPageRoute(builder: (context) => new SettingsScreen()),
  119. );
  120. }
  121. ),
  122. ),
  123. ],
  124. ),
  125. ),
  126. );
  127.  
  128. }
  129. }
  130.  
  131. class CurrencyScreen extends StatelessWidget {
  132. @override
  133. Widget build(BuildContext context) {
  134. return new Scaffold(
  135. appBar: new AppBar(
  136. centerTitle: true,
  137. backgroundColor: Colors.black,
  138. title: new Text("Choose your fiat exchange currency",
  139. style: new TextStyle(
  140. fontFamily: 'PlayfairDisplay',
  141. letterSpacing: 0.8
  142. )
  143. ),
  144. ),
  145. body: new Center(
  146.  
  147. child: new Column(
  148. children: [
  149. new ListTile(
  150. title: new Text('British Pound Sterling',
  151. style: new TextStyle(fontWeight: FontWeight.w500)),
  152.  
  153. leading: new Text('£',
  154. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 30.0)),
  155. ),
  156. new Divider(),
  157. new ListTile(
  158. title: new Text('United States Dollar',
  159. style: new TextStyle(fontWeight: FontWeight.w500)),
  160.  
  161. leading: new Text("US\$",
  162. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 15.0)),
  163. ),
  164. new Divider(),
  165. new ListTile(
  166. title: new Text('Euro',
  167. style: new TextStyle(fontWeight: FontWeight.w500)),
  168.  
  169. leading: new Text("€",
  170. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 30.0)),
  171. ),
  172. new Divider(),
  173. new ListTile(
  174. title: new Text('Japanese yen',
  175. style: new TextStyle(fontWeight: FontWeight.w500)),
  176.  
  177. leading: new Text("¥",
  178. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 30.0)),
  179. ),
  180. new Divider(),
  181. new ListTile(
  182. title: new Text('Australian Dollar',
  183. style: new TextStyle(fontWeight: FontWeight.w500)),
  184.  
  185. leading: new Text("A\$",
  186. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0),
  187. )
  188. ),
  189. new Divider(),
  190. new ListTile(
  191. title: new Text('Canadian Dollar',
  192. style: new TextStyle(fontWeight: FontWeight.w500)),
  193.  
  194. leading: new Text("A\$",
  195. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0),
  196. )
  197. ),
  198. new Divider(),
  199. new ListTile(
  200. title: new Text('Swiss Franc',
  201. style: new TextStyle(fontWeight: FontWeight.w500)),
  202.  
  203. leading: new Text("CHF",
  204. style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 15.0),
  205. )
  206. ),
  207. ],
  208. ),
  209. ),
  210. );
  211.  
  212. }
  213. }
  214.  
  215. class _HomePageState extends State<HomePage> implements CryptoListViewContract {
  216. CryptoListPresenter _presenter;
  217. List<Crypto> _currencies;
  218. bool _isLoading;
  219. final List<MaterialColor> _colors = [Colors.blue, Colors.indigo, Colors.red];
  220.  
  221. _HomePageState() {
  222. _presenter = new CryptoListPresenter(this);
  223. }
  224.  
  225. var refreshKey = new GlobalKey<RefreshIndicatorState>();
  226.  
  227. @override
  228. void initState() {
  229. super.initState();
  230. _isLoading = true;
  231. _presenter.loadCurrencies();
  232. refreshList();
  233. }
  234.  
  235. Future<Null> refreshList() async {
  236.  
  237. refreshKey.currentState?.show(atTop: false);
  238. await new Future.delayed(new Duration(seconds: 1));
  239. _presenter.loadCurrencies();
  240. Scaffold.of(context).showSnackBar(
  241. new SnackBar(
  242. duration: new Duration(seconds: 3),
  243. backgroundColor: Colors.black,
  244. content: new Text(
  245. 'Refresh available every 60 seconds',
  246. textAlign: TextAlign.center,
  247. style: new TextStyle(
  248. letterSpacing: 0.8,
  249. fontFamily: 'PlayfairDisplay'
  250. ),
  251. ),
  252. ),
  253. );
  254. setState(() {
  255. });
  256. return null;
  257. }
  258.  
  259. @override
  260. Widget build(BuildContext context) {
  261. return new Scaffold(
  262. appBar: new AppBar(
  263. centerTitle: true,
  264. backgroundColor: Colors.black,
  265. title: new Text("Cryp-Tick Exchange",
  266. style: new TextStyle(
  267. fontFamily: 'PlayfairDisplay',
  268. letterSpacing: 0.8
  269. )
  270. ),
  271. elevation: defaultTargetPlatform == TargetPlatform.iOS ? 0.0 : 5.0,
  272. ),
  273. drawer: new Drawer(
  274. child: new ListView(
  275. padding: EdgeInsets.zero,
  276. children: <Widget>[
  277. new DrawerHeader(
  278. child: new CircleAvatar(
  279. child: new Image.asset('images/ctavatar.png'),
  280. ),
  281. decoration: new BoxDecoration(
  282. color: Colors.black,
  283. ),
  284. ),
  285. new MaterialButton(
  286. child: new Text(
  287. 'More Info',
  288. textAlign: TextAlign.center,
  289. style: new TextStyle(
  290. fontSize: 20.0,
  291. letterSpacing: 0.8,
  292. fontFamily: 'PlayfairDisplay'
  293. ),
  294. ),
  295. onPressed: (){
  296. Navigator.push(
  297. context,
  298. new MaterialPageRoute(builder: (context) => new MoreInfoScreen()),
  299. );
  300. }
  301. ),
  302. new Divider(),
  303. new MaterialButton(
  304. child: new Text(
  305. 'Settings',
  306. textAlign: TextAlign.center,
  307. style: new TextStyle(
  308. fontSize: 20.0,
  309. letterSpacing: 0.8,
  310. fontFamily: 'PlayfairDisplay'
  311. ),
  312. ),
  313. onPressed: (){
  314. Navigator.push(
  315. context,
  316. new MaterialPageRoute(builder: (context) => new SettingsScreen()),
  317. );
  318. }
  319. ),
  320. ]
  321. ),
  322. ),
  323. body: _isLoading
  324. ? new Center(
  325. child: new CircularProgressIndicator(),
  326. )
  327. : _cryptoWidget()
  328. );
  329.  
  330. }
  331.  
  332.  
  333.  
  334. Widget _cryptoWidget() {
  335. return new Container(
  336. child: new Column(
  337. children: <Widget>[
  338. new Flexible(
  339. child: new ListView.builder(
  340. scrollDirection: Axis.horizontal,
  341. itemBuilder: (BuildContext context, int index) {
  342. return _listViewFiller();
  343. }
  344. ),
  345. ),
  346. new Flexible(
  347. child: new RefreshIndicator(
  348. key: refreshKey,
  349. child: new ListView.builder(
  350. scrollDirection: Axis.horizontal,
  351. itemCount: _currencies.length,
  352. itemBuilder: (BuildContext context, int index) {
  353. final int i = index ~/ 2;
  354. final Crypto currency = _currencies[i];
  355. final MaterialColor color = _colors[i % _colors.length];
  356. if (index.isOdd) {
  357. return new Divider();
  358. }
  359. return _getListItemUi(currency, color);
  360. },
  361. ),
  362. onRefresh:refreshList,
  363. ),
  364. )
  365. ],
  366. )
  367. );
  368. }
  369.  
  370. ListTile _getListItemUi(Crypto currency, MaterialColor color) {
  371. return new ListTile(
  372. leading: new Image.asset("cryptoiconsWhite/"+currency.symbol.toLowerCase()+"@2x.png"),
  373. title: new Text(currency.name,
  374. style: new TextStyle(fontWeight: FontWeight.bold)),
  375. subtitle:
  376. _getSubtitleText(currency.price_usd, currency.percent_change_1h),
  377. isThreeLine: true,
  378. trailing: new IconButton(
  379. icon: new Icon(Icons.add),
  380. onPressed: () async { Directory appDocDir = await getApplicationDocumentsDirectory();
  381. String appDocPath = appDocDir.path;
  382. var myFile = new File('$appDocPath/my_file.txt')
  383. ..writeAsStringSync('myVar: $_currencies');
  384. print(myFile.absolute.path);
  385. }
  386. ),
  387. );
  388. }
  389.  
  390. Card _listViewFiller() {
  391. new BorderRadius.circular(30.0);
  392. return new Card(
  393. child: new SizedBox(
  394. height: 300.0,
  395. width: 300.0,
  396. child: new SizedBox(
  397. child: new Image.network(
  398. 'https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.personaltouchcolorado.com%2Fwp-content%2Fuploads%2F2013%2F01%2FGoodner-Waterfall1-e1362453084645.jpg&f=1'
  399. ),
  400. ),
  401. ),
  402. );
  403. }
  404.  
  405.  
  406. Widget _getSubtitleText(String priceUSD, String percentageChange) {
  407. TextSpan priceTextWidget = new TextSpan(
  408. text: "\$$priceUSD\n", style: new TextStyle(color: Colors.black));
  409. String percentageChangeText = "1 hour: $percentageChange%";
  410. TextSpan percentageChangeTextWidget;
  411.  
  412. if (double.parse(percentageChange) > 0) {
  413. percentageChangeTextWidget = new TextSpan(
  414. text: percentageChangeText,
  415. style: new TextStyle(color: Colors.green ));
  416. } else {
  417. percentageChangeTextWidget = new TextSpan(
  418. text: percentageChangeText, style: new TextStyle(color: Colors.red));
  419. }
  420.  
  421. return new RichText(
  422. text: new TextSpan(
  423. children: [priceTextWidget, percentageChangeTextWidget]));
  424. }
  425.  
  426. @override
  427. void onLoadCryptoComplete(List<Crypto> items) {
  428. // TODO: implement onLoadCryptoComplete
  429.  
  430. setState(() {
  431. _currencies = items;
  432. _isLoading = false;
  433. });
  434. }
  435.  
  436. @override
  437. void onLoadCryptoError() {
  438. // TODO: implement onLoadCryptoError
  439. }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement