Created
July 18, 2020 13:12
-
-
Save mikekosulin/d6ff7ab0857e705b405944893655932a 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 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
class DrawerScreen extends StatefulWidget { | |
@override | |
_DrawerScreenState createState() => _DrawerScreenState(); | |
} | |
List data; | |
class _DrawerScreenState extends State<DrawerScreen> { | |
@override | |
void initState() { | |
super.initState(); | |
} | |
Future getData() async { | |
var response = await http.get(Uri.encodeFull("https://devuz.ru/api/1/catalog"), headers: {"Accept": "application/json"}); | |
return json.decode(utf8.decode(response.bodyBytes)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold(body: _buildNewTiles()); | |
} | |
FutureBuilder _buildNewTiles() { | |
return FutureBuilder( | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.none && snapshot.hasData == null) { | |
return Container( | |
child: Text('Nothing to show'), | |
); | |
} | |
return ListView.builder( | |
itemCount: snapshot.data.length, | |
itemBuilder: (context, index) { | |
var element = snapshot.data[index]; | |
return _buildTile(element); | |
}, | |
); | |
}, | |
future: getData(), | |
); | |
} | |
_buildTile(element) { | |
return ExpansionTile(title: Text(element['name']), children: _buildChildren(element['children'])); | |
} | |
List<Widget> _buildChildren(List children) { | |
List<Widget> childCategories = []; | |
children.forEach((childCategory) { | |
childCategories.add(ListTile( | |
dense: true, | |
title: Text("@" + childCategory["name"]), | |
subtitle: Text(childCategory["name"]), | |
)); | |
}); | |
return childCategories; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
проверку на null добавь и не будет валиться до загрузки