【发布时间】:2020-01-07 03:08:36
【问题描述】:
我想在我的 Flutter 应用中使用这个 Flutter 树视图小部件来构建公司树视图
https://pub.dev/packages/tree_view
我有一个带有树结构公司列表的网络服务。 https://washservice.com/api/companyXML/1fe5bae2-331a-4080-b34f-5ebd3518efd8
我已经编写了带有递归函数的 json 解析代码来构建树视图,但它不起作用。有人可以帮我解决解析问题并构建树视图小部件
这是我的代码
import 'dart:async';
import 'dart:convert';
import 'package:example/models/Company.dart';
import 'package:example/widgets/directory_widget.dart';
import 'package:example/widgets/file_widget.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:tree_view/tree_view.dart';
class CompaniesPage extends StatefulWidget {
CompaniesPage({Key key, this.title}) : super(key: key);
final String title;
@override
_CompaniesPageState createState() => _CompaniesPageState();
}
class _CompaniesPageState extends State<CompaniesPage> {
List<Company> companiesList = new List<Company>();
@override
void initState() {
super.initState();
// Loading initial data or first request to get the data
_getTeeViewData1();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title ?? 'Tree View demo'),
),
body: Center(
child: TreeView(
startExpanded: false,
children: _getChildList(companiesList),
),
),
);
}
// Webservice request to load 20 users data using paging
Future<List<Company>> _getTeeViewData1() async {
String url =
"https://washservice.com/api/companyXML/1fe5bae2-331a-4080-b34f-5ebd3518efd8";
print(url);
var response = await http.get(url);
var jsonData = json.decode(response.body);
print(jsonData);
var data = jsonData["Companies"];
var companies = data["Company"];
print(companies);
Company c = new Company();
c.CompanyId = companies["CompanyId"];
c.CompanyName = companies["CompanyName"];
c.ParentId = companies["ParentId"];
c.CostCenter = '${companies["CostCenter"] ?? ""}';
c.IsSelectableforMovement = companies["IsSelectableforMovement"];
c = getChildCompanies(companies["Company"], c);
companiesList.add(c);
return companiesList;
}
Company getChildCompanies(childCompanies, parentCompany) {
if (childCompanies != null) {
for (var childCompany in childCompanies) {
Company childCO = new Company();
childCO.CompanyId = childCompany["CompanyId"];
childCO.CompanyName = childCompany["CompanyName"];
childCO.ParentId = childCompany["ParentId"];
childCO.CostCenter = '${childCompany["CostCenter"] ?? ""}';
childCO.IsSelectableforMovement =
childCompany["IsSelectableforMovement"];
Company c2 = getChildCompanies(childCompany["Company"], childCO);
parentCompany.company.add(c2);
return parentCompany;
}
}
}
List<Widget> _getChildList(List<Company> childDocuments) {
return childDocuments.map((document) {
if (document.company.length != 0) {
return Container(
margin: EdgeInsets.only(left: 8),
child: TreeViewChild(
parent: _getDocumentWidget(document: document),
children: _getChildList(document.company),
),
);
}
return Container(
margin: const EdgeInsets.only(left: 4.0),
child: _getDocumentWidget(document: document),
);
}).toList();
}
Widget _getDocumentWidget({@required Company document}) =>
document.company.length == 0
? _getFileWidget(document: document)
: _getDirectoryWidget(document: document);
DirectoryWidget _getDirectoryWidget({@required Company document}) =>
DirectoryWidget(directoryName: document.CompanyName);
FileWidget _getFileWidget({@required Company document}) =>
FileWidget(fileName: document.CompanyName);
}
公司.dart
class Company {
Company();
String CompanyId;
String CompanyName;
String ParentId;
String CostCenter;
String IsSelectableforMovement;
List<Company> company = new List<Company>();
}
【问题讨论】: