flutter - 在颤动中使用扩展和非扩展瓷砖
问题描述
我需要生成需要扩展和非扩展磁贴的列表
这是我的代码
return ListView.builder(
itemCount: categoryAllListData.length,
itemBuilder: (BuildContext context, int index) => EntryItem(
categoryAllListData[index],
));
class EntryItem extends StatelessWidget {
// const CategoryAllListModel(this.name,this.catlistt);
// final Catlist catlist;
final CategoryAllListModel categoryAllListModel;
const EntryItem(this.categoryAllListModel);
// This function recursively creates the multi-level list rows.
Widget _buildTiles(CategoryAllListModel root) {
if (root.catlist.isEmpty) {
return ListTile(
title: Text(root.name),
);
}
return ExpansionTile(
key: PageStorageKey<CategoryAllListModel>(root),
title: Text(root.name),
children: root.catlist.map<Widget>(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(categoryAllListModel);
}
}
但我在儿童中遇到错误:root.catlist.map( _buildTiles ).toList(),
参数类型“Widget Function(CategoryAllListModel)”不能分配给参数类型“Widget Function(Catlist)”。dartargument_type_not_assignable
class CategoryAllListModel {
int id;
String name;
List<Catlist> catlist;
CategoryAllListModel({
this.id,
this.name,
this.catlist,
});
CategoryAllListModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
//price = json['price'].toDouble();
catlist = List<Catlist>.from(
json["children_categories"].map((x) => Catlist.fromJson(x)));
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['children_categories'] =
List<dynamic>.from(catlist.map((x) => x.toJson()));
return data;
}
}
class Catlist {
int id;
String name;
Catlist({this.id, this.name});
Catlist.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
return data;
}
}
解决方案
您可以在下面复制粘贴运行完整代码
请使用List<CategoryAllListModel> catlist;
在您的情况下,您不需要class Catlist
代码片段
class CategoryAllListModel {
const CategoryAllListModel(this.name,
[this.catlist = const <CategoryAllListModel>[]]);
final String name;
final List<CategoryAllListModel> catlist;
}
const List<CategoryAllListModel> categoryAllListData = <CategoryAllListModel>[
CategoryAllListModel(
'Chapter A',
<CategoryAllListModel>[
CategoryAllListModel(
'Section A0',
<CategoryAllListModel>[
CategoryAllListModel('Item A0.1'),
CategoryAllListModel('Item A0.2'),
],
),
CategoryAllListModel('Section A1'),
CategoryAllListModel('Section A2'),
],
),
CategoryAllListModel(
'Chapter B',
<CategoryAllListModel>[
CategoryAllListModel('Section B0'),
CategoryAllListModel('Section B1'),
],
),
];
工作演示
完整代码
import 'package:flutter/material.dart';
class CategoryAllListModel {
const CategoryAllListModel(this.name,
[this.catlist = const <CategoryAllListModel>[]]);
final String name;
final List<CategoryAllListModel> catlist;
}
const List<CategoryAllListModel> categoryAllListData = <CategoryAllListModel>[
CategoryAllListModel(
'Chapter A',
<CategoryAllListModel>[
CategoryAllListModel(
'Section A0',
<CategoryAllListModel>[
CategoryAllListModel('Item A0.1'),
CategoryAllListModel('Item A0.2'),
],
),
CategoryAllListModel('Section A1'),
CategoryAllListModel('Section A2'),
],
),
CategoryAllListModel(
'Chapter B',
<CategoryAllListModel>[
CategoryAllListModel('Section B0'),
CategoryAllListModel('Section B1'),
],
),
];
class CategoryAllListModelItem extends StatelessWidget {
// const CategoryAllListModel(this.name,this.catlistt);
// final Catlist catlist;
final CategoryAllListModel categoryAllListModel;
const CategoryAllListModelItem(this.categoryAllListModel);
// This function recursively creates the multi-level list rows.
Widget _buildTiles(CategoryAllListModel root) {
if (root.catlist.isEmpty) {
return ListTile(
title: Text(root.name),
);
}
return ExpansionTile(
key: PageStorageKey<CategoryAllListModel>(root),
title: Text(root.name),
children: root.catlist.map<Widget>(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(categoryAllListModel);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: categoryAllListData.length,
itemBuilder: (BuildContext context, int index) =>
CategoryAllListModelItem(
categoryAllListData[index],
)));
}
}
推荐阅读
- c++ - OpenGL结合Yaw Pitch Roll
- reactjs - 初始化reactjs useState useTracker Subscriptions Form state meteor 之前无法访问___
- c++ - 将 CMake 标志传递给 VS 代码“任务”?
- node.js - 有没有办法用 express 和 node 删除 Mongodb 数据库中的嵌套列表项?
- recursion - 递归堆栈溢出与 F# 中的异常
- vim - 如何设置由 vim :sh 命令生成的 shell 的环境变量
- oracle - 查询查找主键和外键列以及代理键 oracle sql
- powerbi - Dax TotalYTD 和 SamePeriodLastYear 不工作
- python - 是否有可能只有一个 while 循环的运行时间为 O(n)?
- r - 如何使用R中的stringr将一位数字替换为两位数字