我有这个账户类
import 'package:project/models/category_model.dart';
enum AccountTypes {
cash,
banks,
}
class Account {
AccountTypes type;
double value;
List<BalnceCategory>? categories;
Account({
required this.type,
required this.value,
this.categories,
});
Map<String, dynamic> toJSON() {
return {
"type": type,
"value": value,
"categories": categories,
};
}
}
Map<AccountTypes, List<dynamic>> accounts = {
AccountTypes.cash: [
BalnceCategory(image: "food.png", title: "Food", value: 412.5).toJSON(),
BalnceCategory(image: "shopping.png", title: "Shopping", value: 412.5).toJSON(),
],
AccountTypes.banks: [
BalnceCategory(image: "food.png", title: "Food", value: 1242.63).toJSON(),
BalnceCategory(image: "shopping.png", title: "Shopping", value: 1242.63).toJSON(),
]
};
每个帐户都应该包含一个BalnceCategory列表。
class BalnceCategory {
String image;
String title;
double value;
BalnceCategory({
required this.image,
required this.title,
required this.value,
});
Map<String, dynamic> toJSON() {
return {
"image": image,
"title": title,
"value": value,
};
}
}
现在,我想在两个部分中显示这个Map Map<AccountTypes, List<dynamic>> accounts
.,我将把这个映射称为sections。所以在第一节中,我想列出所有可用的帐户,比如一个行,每个帐户都有一个按钮,所以我所做的就是通过像这个accounts.entries.map
这样的帐户映射,并为每个帐户返回一个按钮,这些按钮可以用它的索引设置一个名为currentIndex
的状态。
现在,在第二节中,我希望根据状态值列出所有帐户类别,例如,如果currentIndex
值是0
,我希望显示现金帐户中的所有类别,如果currentIndex
值是1
,则希望显示银行帐户中的所有类别。
到目前为止,我所做的按钮部分,我是正常工作,我的问题是在第二部分。我试过这样做
Expanded(
child: GridView.builder(
physics: const BouncingScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: mainUnit / 2,
crossAxisSpacing: mainUnit / 2,
childAspectRatio: 3 / 4,
),
itemCount: accounts.keys.length,
itemBuilder: (context, index) {
return accounts.forEach((key, value) {
if (key.index == currentIndex) {
value.map((e) => {Text(e.toString())});
}
});
},
),
),
但是它给了我一个错误:The return type 'void' isn't a 'Widget', as required by the closure's context.
发布于 2022-11-09 17:09:46
ItemBuilder应该返回一个小部件,然后返回accounts.forEach(.)这是一个空函数( forEach()是一个空函数/闭包)。
试试这个:
Text( accounts.keys.firstWhere( (item) => item.index == currentIndex,
orElse: ()=> "",).toString() );
但是!等等!你为什么不看看https://pub.dev/packages/flutter_sticky_header
您应该实现您需要的东西:它显示一个标题,在您的例子中,它可以是一个字符串AccountTypes.cash.toString()或AccountTypes.banks.toString( ),然后遵循这个示例,您应该获得所需的东西(为AccountTypes显示分组卡)。还有,我建议你用
https://pub.dev/packages/freezed
它帮助您定义数据类,并将其序列化为JSON。
例如:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'account.g.dart';
part 'account.freezed.dart';
@freezed()
class Account with _$Account {
factory Account.cash({
@Default(0) double? value;
List<BalnceCategory>? categories;
}) = CashAccount;
factory Account.bank({
@Default(0) double? value;
List<BalnceCategory>? categories;
}) = BankAccount;
factory Account.fromJson(Map<String, dynamic> json) =>
_$AccountFromJson(json);
}
以这种方式,您将拥有数据类(帐户)及其类型(现金/银行)和可序列化。
要创建一个帐户很容易:
var myCash = Account.cash(... , ... );
CashAccount和BankAccount都是实现帐户(抽象类)的两个不同的类,您可以在一个简单的帐户列表中使用它。然后,为了节省您可以使用的银行/现金:
var myAccount = Account.cash(....);
myAccount.when(
bank: (BankAccount account) => ....,
cash: (CashAccount account) => .... ,
),
https://stackoverflow.com/questions/74378597
复制相似问题