首页 > 解决方案 > 在颤动中使用扩展和非扩展瓷砖

问题描述

我需要生成需要扩展和非扩展磁贴的列表

这是我的代码

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;
  }
}

标签: flutter

解决方案


您可以在下面复制粘贴运行完整代码
请使用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],
                )));
  }
}

推荐阅读