【问题标题】:working with expansion and non expansion tile in flutter在颤动中使用扩展和非扩展瓷砖
【发布时间】:2021-04-10 20:51:57
【问题描述】:

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

这是我的代码

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


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    请使用List&lt;CategoryAllListModel&gt; catlist;
    在您的情况下,您不需要class Catlist
    代码sn-p

    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],
                    )));
      }
    }
    

    【讨论】:

    • 嗨,谢谢,我已经添加了我的模型。请帮助解决这个问题
    • 请使用 List catlist;你不需要类 Catlist
    • 能否展示您的重现代码和 json 字符串,需要重现您的解析 json 字符串。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2020-06-15
    • 2013-03-13
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多