From 51ab43e5dab1df1d212fafc8faa148583766254a Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 2 Apr 2019 20:48:56 +0200 Subject: [PATCH] add yaml parser and display result in a list view --- android/gradle.properties | 2 +- lib/main.dart | 117 +++++++++++++++++++++++--------------- pubspec.yaml | 2 + 3 files changed, 74 insertions(+), 47 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index 8bd86f6..2d33552 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1 +1 @@ -org.gradle.jvmargs=-Xmx1536M +org.gradle.jvmargs=-Xmx4g diff --git a/lib/main.dart b/lib/main.dart index f4ebf1d..78d80bd 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,10 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:path_provider/path_provider.dart'; + +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:yaml/yaml.dart'; void main() => runApp(MyApp()); @@ -20,7 +26,7 @@ class MyApp extends StatelessWidget { // is not restarted. primarySwatch: Colors.blue, ), - home: MyHomePage(title: 'Flutter Demo Home Page'), + home: MyHomePage(title: 'DevFest'), ); } } @@ -44,17 +50,18 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - int _counter = 0; + static var httpClient = new HttpClient(); - void _incrementCounter() { - 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++; - }); + Future _downloadFile(String url, String filename) async { + var request = await httpClient.getUrl(Uri.parse(url)); + var response = await request.close(); + var bytes = await consolidateHttpClientResponseBytes(response); + String dir = (await getApplicationDocumentsDirectory()).path; + File file = new File('$dir/$filename'); + await file.writeAsBytes(bytes); + var content = await readFile(file); + var doc = loadYaml(content); + return doc; } @override @@ -71,41 +78,59 @@ class _MyHomePageState extends State { // 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 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: [ - Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.display1, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. + body: FutureBuilder( + future: _downloadFile( + "https://raw.githubusercontent.com/GDG-Zagreb/zeppelin/gh-pages/_data/sessions.yml", + "sessions.yml"), + builder: (context, response) { + print(response.data); + return ListView.builder( + itemCount: response.data.length, + itemBuilder: (context, index) { + var item = response.data[index]; + String subtype = item["subtype"]; + String complexity = item["complexity"]; + var color; + switch (complexity) { + case "Beginner": + color = Colors.green[800]; + break; + case "Intermediate": + color = Colors.yellow[800]; + break; + default: + color = Colors.blue[800]; + break; + } + return ListTile( + title: Row( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Expanded( + child: Padding( + padding: const EdgeInsets.only(right: 8.0), + child: Text(item["title"]), + )), + ]), + subtitle: Text( + item["description"] != null + ? item["description"] + : '', + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + trailing: Icon(Icons.hearing, color: color), + onTap: () { + print(item["title"]); + }, + ); + }); + }), ); } + + Future readFile(File stored) async { + String contents = await stored.readAsString(); + return contents; + } } diff --git a/pubspec.yaml b/pubspec.yaml index 875032c..1265107 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -19,6 +19,8 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 + path_provider: ^0.5.0 + yaml: ^2.1.15 dev_dependencies: flutter_test: