Skip to content

Instantly share code, notes, and snippets.

@jadsonbr
Created January 9, 2020 02:34
Show Gist options
  • Save jadsonbr/18f33f189a12cdb70f2b688b730bdc27 to your computer and use it in GitHub Desktop.
Save jadsonbr/18f33f189a12cdb70f2b688b730bdc27 to your computer and use it in GitHub Desktop.
Flutter TreeView
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:tree_view/tree_view.dart';
import 'dart:collection';
class Folder {
String descricao;
String id;
Folder({this.descricao, this.id});
}
class TreePage extends StatefulWidget {
@override
_TreePageState createState() => _TreePageState();
}
class _TreePageState extends State<TreePage> {
String responseBody = '{"children":[{"children":[{"children":[{"children":[{"children":[],"deleted_at":null,"depth":4,"descricao":"ALA 1","id":11,"pai":10},{"children":[],"deleted_at":null,"depth":4,"descricao":"ALA 2","id":12,"pai":10}],"deleted_at":null,"depth":3,"descricao":"SALA 201","id":10,"pai":9}],"deleted_at":null,"depth":2,"descricao":"FEVEREIRO","id":9,"pai":7},{"children":[{"children":[],"deleted_at":null,"depth":1,"descricao":"SALA 01","id":13,"pai":8},{"children":[],"deleted_at":null,"depth":1,"descricao":"SALA 02","id":14,"pai":8}],"deleted_at":null,"descricao":"JANEIRO","id":8,"pai":7}],"deleted_at":null,"depth":1,"descricao":"TRUNFO","id":7,"pai":1},{"children":[{"children":[],"deleted_at":null,"depth":1,"descricao":"01 - JANEIRO","id":3,"pai":2},{"children":[],"deleted_at":null,"depth":1,"descricao":"02 - FEVEREIRO","id":4,"pai":2},{"children":[],"deleted_at":null,"depth":1,"descricao":"03 - MARÇO","id":5,"pai":2}],"deleted_at":null,"descricao":"LAMINA","id":2,"pai":1},{"children":[],"deleted_at":null,"descricao":"TESTE","id":15,"pai":1}],"depth":0,"descricao":"MEU ESPACO","id":1,"pai":null,"state":{"expanded":true,"selected":true}}';
@override
Widget build(BuildContext context) {
Map mapBody = jsonDecode(responseBody);
return Scaffold(
appBar: AppBar(
title: new Text(
"Pastas",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
foreground: Paint()..color = Colors.white),
overflow: TextOverflow.ellipsis,
),
leading: Builder(
builder: (context) => IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();
},
),
),
),
body: ListView(shrinkWrap: true, children: <Widget>[
printGroupTree(
mapBody,
),
]),
);
}
Widget printGroupTree(Map group, {double level = 0, String id}) {
// List<Folder> e = [];
if (group['children'] != null) {
List<Widget> subGroups = List<Widget>();
// for (Map subGroup in group['children']) {
// e.add(Folder(descricao: subGroup['descricao'], id: subGroup['id'].toString()));
// }
// List<Folder> items = e.toList(growable: false);
// Comparator<Folder> priceComparator = (a, b) => a.descricao.compareTo(b.descricao);
// items.sort(priceComparator);
// print(items);
for (Map subGroup in group['children']) {
subGroups.add(
printGroupTree(subGroup,
level: level + 1, id: subGroup['id'].toString()),
);
}
return Parent(
parent: _card(group['descricao'], level * 20, group['id'].toString()),
childList: ChildList(
children: subGroups,
),
);
} else {
return _card(group['descricao'], level * 20, group['id'].toString());
}
}
Widget _card(
String groupName,
double leftPadding,
String id,
) {
return Container(
padding: EdgeInsets.only(
left: leftPadding + 5,
right: 15,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50.0),
),
height: 50,
child: Row(
children: <Widget>[
Container(
width: 250,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Flexible(
child: Text(
groupName,
),
),
],
),
),
Expanded(
child: SizedBox(),
),
InkWell(
onTap: () {},
child: new IconButton(
icon: new Icon(Icons.remove_red_eye),
iconSize: 20,
onPressed: () {
},
),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment