Flutter 中的 ExpansionTile
ExpansionTile in flutter
我正在尝试根据从后端获得的数据构建扩展图块,而来自后端的数据看起来像
var sampleresponse = [
{"Total Income": 500.0},
{"Total Expense": 1000.0},
{"Maintenance": 500.0},
{"Tank Water": 100.0, "truck Charges": 900.0},
];
var sampletotalIncome = sampleresponse[0];
var sampletotalExpense = sampleresponse[1];
var sampledetailIncome = sampleresponse[2];
var sampledetailExpense = sampleresponse[3];
var sampletotalIncomeKeys = sampletotalIncome.keys;
var sampletotalExpenseKeys = sampletotalExpense.keys;
var sampledetailIncomeKeys = sampledetailIncome.Keys;
var sampledetailExpenseKeys = sampledetailExpense.keys;
var sampletotalIncomevalue = sampletotalIncome.value;
var sampletotalExpensevalue = sampletotalExpense.value;
var sampledetailIncomevalue = sampledetailIncome.value;
var sampledetailExpensevalue= sampledetailExpense.value;
//sampletotalIncomeKeys is [Total Income]
//sampletotalIncomevalue is [500.0]
//sampletotalExpenseKeys is [Total Expense]
//sampletotalExpensevalue is [1000.0]
//sampledetailIncomeKeys is [Maintanence]
//sampledetailIncomevalue is [500.0]
//sampledetailExpenseKeys is [Tank Water, truck Charges]
//sampledetailedExpensevalue is [100.0, 900.0]
其中 "Maintenance":500.0 应放在 "Total Income" 下,"Total Expense" 应包含 "Tank Water" 和 "truck Charges"。这是我的代码:
来自 ExpansionTile.dart 的片段:
import 'package:flutter/material.dart';
class StuffInTiles extends StatelessWidget {
final MyTile myTile;
StuffInTiles(this.myTile);
@override
Widget build(BuildContext context) {
return _buildTiles(myTile);
}
Widget _buildTiles(MyTile t) {
if (t.children.isEmpty)
return new ListTile(
title: new Text(
t.title,
style: new TextStyle(color: Colors.white),
),
trailing: new Text(
t.value,
style: new TextStyle(color: Colors.white),
),
);
return new ExpansionTile(
key: new PageStorageKey<MyTile>(t),
title: new Text(
t.title,
style: new TextStyle(color: Colors.white),
),
trailing: new Text(
t.value,
style: new TextStyle(color: Colors.white),
),
children: t.children.map(_buildTiles).toList(),
);
}
}
class MyTile {
final String title;
final String value;
final List<MyTile> children;
MyTile(this.title, this.value, [this.children = const <MyTile>[]]);
}
}
来自 Main.dart 的片段:
List<MyTile> itemList = <MyTile>[
new MyTile(
sampletotalIncomeKeys.toString(),
sampletotalIncomevalue.toString(),
sampledetailIncomeKeys
.map(
(items) => new
MyTile(sampledetailIncomeKeys.toString(),
sampledetailIncomevalue.toString()),
)
.toList(),
),
new MyTile(
sampletotalExpenseKeys.toString(),
sampletotalExpensevalue.toString(),
sampledetailedExpense.map((items, value) =>
MyTile(items.toString(), value.toString()).toString(),
).toString()
),
];
...........
new Expanded(
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new StuffInTiles(itemList[index]);
},
itemCount: itemList.length,
),
)
有了这个我得到一个错误
"The argument type "String" 不能分配给参数类型列表。我不明白这一点。有人能帮我解释一下这个的逻辑吗?
谢谢!
你的代码在这部分是错误的:
List<MyTile> itemList = <MyTile>[
new MyTile(
sampletotalIncomeKeys.toString(),
sampletotalIncomevalue.toString(),
sampledetailIncomeKeys
.map(
(items) => new
MyTile(sampledetailIncomeKeys.toString(),
sampledetailIncomevalue.toString()),
)
.toList(),
),
new MyTile(
sampletotalExpenseKeys.toString(),
sampletotalExpensevalue.toString(),
sampledetailedExpense.map((items, value) =>
MyTile(items.toString(), value.toString()).toString(),
).toString()
),
];
你的第二个对象的最后一个参数是错误的,它应该是 List
而不是 String
,用这个代码替换:
List<MyTile> itemList = <MyTile>[
new MyTile(
sampletotalIncomeKeys.toString(),
sampletotalIncomevalue.toString(),
sampledetailIncomeKeys
.map(
(items) => new MyTile(sampledetailIncomeKeys.toString(),
sampledetailIncomevalue.toString()),
)
.toList(),
),
new MyTile(
sampletotalExpenseKeys.toString(),
sampletotalExpensevalue.toString(),
sampledetailedExpense.keys
.map(
(key) =>
MyTile(key, sampledetailedExpense[key].toString()),
)
.toList()),
];
}
我正在尝试根据从后端获得的数据构建扩展图块,而来自后端的数据看起来像
var sampleresponse = [
{"Total Income": 500.0},
{"Total Expense": 1000.0},
{"Maintenance": 500.0},
{"Tank Water": 100.0, "truck Charges": 900.0},
];
var sampletotalIncome = sampleresponse[0];
var sampletotalExpense = sampleresponse[1];
var sampledetailIncome = sampleresponse[2];
var sampledetailExpense = sampleresponse[3];
var sampletotalIncomeKeys = sampletotalIncome.keys;
var sampletotalExpenseKeys = sampletotalExpense.keys;
var sampledetailIncomeKeys = sampledetailIncome.Keys;
var sampledetailExpenseKeys = sampledetailExpense.keys;
var sampletotalIncomevalue = sampletotalIncome.value;
var sampletotalExpensevalue = sampletotalExpense.value;
var sampledetailIncomevalue = sampledetailIncome.value;
var sampledetailExpensevalue= sampledetailExpense.value;
//sampletotalIncomeKeys is [Total Income]
//sampletotalIncomevalue is [500.0]
//sampletotalExpenseKeys is [Total Expense]
//sampletotalExpensevalue is [1000.0]
//sampledetailIncomeKeys is [Maintanence]
//sampledetailIncomevalue is [500.0]
//sampledetailExpenseKeys is [Tank Water, truck Charges]
//sampledetailedExpensevalue is [100.0, 900.0]
其中 "Maintenance":500.0 应放在 "Total Income" 下,"Total Expense" 应包含 "Tank Water" 和 "truck Charges"。这是我的代码:
来自 ExpansionTile.dart 的片段:
import 'package:flutter/material.dart';
class StuffInTiles extends StatelessWidget {
final MyTile myTile;
StuffInTiles(this.myTile);
@override
Widget build(BuildContext context) {
return _buildTiles(myTile);
}
Widget _buildTiles(MyTile t) {
if (t.children.isEmpty)
return new ListTile(
title: new Text(
t.title,
style: new TextStyle(color: Colors.white),
),
trailing: new Text(
t.value,
style: new TextStyle(color: Colors.white),
),
);
return new ExpansionTile(
key: new PageStorageKey<MyTile>(t),
title: new Text(
t.title,
style: new TextStyle(color: Colors.white),
),
trailing: new Text(
t.value,
style: new TextStyle(color: Colors.white),
),
children: t.children.map(_buildTiles).toList(),
);
}
}
class MyTile {
final String title;
final String value;
final List<MyTile> children;
MyTile(this.title, this.value, [this.children = const <MyTile>[]]);
}
}
来自 Main.dart 的片段:
List<MyTile> itemList = <MyTile>[
new MyTile(
sampletotalIncomeKeys.toString(),
sampletotalIncomevalue.toString(),
sampledetailIncomeKeys
.map(
(items) => new
MyTile(sampledetailIncomeKeys.toString(),
sampledetailIncomevalue.toString()),
)
.toList(),
),
new MyTile(
sampletotalExpenseKeys.toString(),
sampletotalExpensevalue.toString(),
sampledetailedExpense.map((items, value) =>
MyTile(items.toString(), value.toString()).toString(),
).toString()
),
];
...........
new Expanded(
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new StuffInTiles(itemList[index]);
},
itemCount: itemList.length,
),
)
有了这个我得到一个错误
"The argument type "String" 不能分配给参数类型列表。我不明白这一点。有人能帮我解释一下这个的逻辑吗?
谢谢!
你的代码在这部分是错误的:
List<MyTile> itemList = <MyTile>[
new MyTile(
sampletotalIncomeKeys.toString(),
sampletotalIncomevalue.toString(),
sampledetailIncomeKeys
.map(
(items) => new
MyTile(sampledetailIncomeKeys.toString(),
sampledetailIncomevalue.toString()),
)
.toList(),
),
new MyTile(
sampletotalExpenseKeys.toString(),
sampletotalExpensevalue.toString(),
sampledetailedExpense.map((items, value) =>
MyTile(items.toString(), value.toString()).toString(),
).toString()
),
];
你的第二个对象的最后一个参数是错误的,它应该是 List
而不是 String
,用这个代码替换:
List<MyTile> itemList = <MyTile>[
new MyTile(
sampletotalIncomeKeys.toString(),
sampletotalIncomevalue.toString(),
sampledetailIncomeKeys
.map(
(items) => new MyTile(sampledetailIncomeKeys.toString(),
sampledetailIncomevalue.toString()),
)
.toList(),
),
new MyTile(
sampletotalExpenseKeys.toString(),
sampletotalExpensevalue.toString(),
sampledetailedExpense.keys
.map(
(key) =>
MyTile(key, sampledetailedExpense[key].toString()),
)
.toList()),
];
}