Created
August 10, 2019 08:58
-
-
Save juliuscanute/8b0b5a1a5962468a1763e3ad2674fc9a to your computer and use it in GitHub Desktop.
[Handling JSON Data] #flutter #pubspec #json #orientation #fetch #list
This file contains hidden or 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
{ | |
"H1": [ | |
{ | |
"K1": "V1", | |
"K2": 1.0 | |
}, | |
{ | |
"K1": "V2", | |
"K2": 2.0 | |
} | |
], | |
"H2": [ | |
{ | |
"K1": "V3", | |
"K2": 1.0 | |
}, | |
{ | |
"K1": "V4", | |
"K2": 2.0 | |
} | |
] | |
} |
This file contains hidden or 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
assets: | |
- assets/data/my_json_data.json |
This file contains hidden or 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 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'backdrop.dart'; | |
import 'category.dart'; | |
import 'category_tile.dart'; | |
import 'unit.dart'; | |
import 'unit_converter.dart'; | |
class CategoryRoute extends StatefulWidget { | |
const CategoryRoute(); | |
@override | |
_CategoryRouteState createState() => _CategoryRouteState(); | |
} | |
class _CategoryRouteState extends State<CategoryRoute> { | |
Category _defaultCategory; | |
Category _currentCategory; | |
final _categories = <Category>[]; | |
static const _baseColors = <ColorSwatch>[ | |
ColorSwatch(0xFF6AB7A8, { | |
'highlight': Color(0xFF6AB7A8), | |
'splash': Color(0xFF0ABC9B), | |
}), | |
ColorSwatch(0xFFFFD28E, { | |
'highlight': Color(0xFFFFD28E), | |
'splash': Color(0xFFFFA41C), | |
}), | |
ColorSwatch(0xFFFFB7DE, { | |
'highlight': Color(0xFFFFB7DE), | |
'splash': Color(0xFFF94CBF), | |
}), | |
ColorSwatch(0xFF8899A8, { | |
'highlight': Color(0xFF8899A8), | |
'splash': Color(0xFFA9CAE8), | |
}), | |
ColorSwatch(0xFFEAD37E, { | |
'highlight': Color(0xFFEAD37E), | |
'splash': Color(0xFFFFE070), | |
}), | |
ColorSwatch(0xFF81A56F, { | |
'highlight': Color(0xFF81A56F), | |
'splash': Color(0xFF7CC159), | |
}), | |
ColorSwatch(0xFFD7C0E2, { | |
'highlight': Color(0xFFD7C0E2), | |
'splash': Color(0xFFCA90E5), | |
}), | |
ColorSwatch(0xFFCE9A9A, { | |
'highlight': Color(0xFFCE9A9A), | |
'splash': Color(0xFFF94D56), | |
'error': Color(0xFF912D2D), | |
}), | |
]; | |
@override | |
Future<void> didChangeDependencies() async { | |
super.didChangeDependencies(); | |
if (_categories.isEmpty) { | |
await _retrieveLocalCategories(); | |
} | |
} | |
Future<void> _retrieveLocalCategories() async { | |
final json = DefaultAssetBundle | |
.of(context) | |
.loadString('assets/data/regular_units.json'); | |
final data = JsonDecoder().convert(await json); | |
if (data is! Map) { | |
throw ('Data retrieved from API is not a Map'); | |
} | |
var categoryIndex = 0; | |
data.keys.forEach((key) { | |
final List<Unit> units = | |
data[key].map<Unit>((dynamic data) => Unit.fromJson(data)).toList(); | |
var category = Category( | |
name: key, | |
units: units, | |
color: _baseColors[categoryIndex], | |
iconLocation: Icons.cake, | |
); | |
setState(() { | |
if (categoryIndex == 0) { | |
_defaultCategory = category; | |
} | |
_categories.add(category); | |
}); | |
categoryIndex += 1; | |
}); | |
} | |
void _onCategoryTap(Category category) { | |
setState(() { | |
_currentCategory = category; | |
}); | |
} | |
Widget _buildCategoryWidgets(Orientation deviceOrientation) { | |
if (deviceOrientation == Orientation.portrait) { | |
return ListView.builder( | |
itemBuilder: (BuildContext context, int index) { | |
return CategoryTile( | |
category: _categories[index], | |
onTap: _onCategoryTap, | |
); | |
}, | |
itemCount: _categories.length, | |
); | |
} else { | |
return GridView.count( | |
crossAxisCount: 2, | |
childAspectRatio: 3.0, | |
children: _categories.map((Category c) { | |
return CategoryTile( | |
category: c, | |
onTap: _onCategoryTap, | |
); | |
}).toList(), | |
); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
if (_categories.isEmpty) { | |
return Center( | |
child: Container( | |
height: 180.0, | |
width: 180.0, | |
child: CircularProgressIndicator(), | |
), | |
); | |
} | |
assert(debugCheckHasMediaQuery(context)); | |
final listView = Padding( | |
padding: EdgeInsets.only( | |
left: 8.0, | |
right: 8.0, | |
bottom: 48.0, | |
), | |
child: _buildCategoryWidgets(MediaQuery.of(context).orientation), | |
); | |
return Backdrop( | |
currentCategory: | |
_currentCategory == null ? _defaultCategory : _currentCategory, | |
frontPanel: _currentCategory == null | |
? UnitConverter(category: _defaultCategory) | |
: UnitConverter(category: _currentCategory), | |
backPanel: listView, | |
frontTitle: Text('Unit Converter'), | |
backTitle: Text('Select a Category'), | |
); | |
} | |
} |
This file contains hidden or 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
//SYNTAX | |
class $1 extends StatefulWidget { | |
const $1(); | |
@override | |
_$2 createState() => _$2(); | |
} | |
class $2 extends State<$1> { | |
const $2(); | |
//Called when a dependency of this State object changes. Some subclasses do override this | |
//method because they need to do some expensive work (e.g., network fetches) when their | |
//dependencies change, and that work would be too expensive to do for every build. | |
@override | |
Future<void> didChangeDependencies() async { | |
super.didChangeDependencies(); | |
if (_entries.isEmpty) { | |
await _retrieveMyData(); | |
} | |
} | |
Future<void> _retrieveMyData() async { | |
final json = DefaultAssetBundle | |
.of(context) | |
.loadString('assets/data/my_json_data.json'); | |
final data = JsonDecoder().convert(await json); | |
if (data is! Map) { | |
throw ('Data retrieved from API is not a Map'); | |
} | |
var entryIndex = 0; | |
data.keys.forEach((key) { | |
final List<Unit> units = | |
data[key].map<Unit>((dynamic data) => Unit.fromJson(data)).toList(); | |
var data = MyDataClass( | |
name: key, | |
units: units, | |
); | |
setState(() { | |
if (categoryIndex == 0) { | |
_defaultCategory = data; | |
} | |
_entries.add(data); | |
}); | |
entryIndex += 1; | |
}); | |
} | |
Widget build(BuildContext context) { | |
return Widget(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment