如何使用GroupedListView / ListView.builder对卡中的listTiles进行分组?

时间:2020-08-09 20:38:00

标签: flutter listview dart card

我想要实现的目标:我尝试创建一个GroupedListView,其中将项目按日期分组(到目前为止,GroupedListView小部件的使用情况很好)然后显示单个卡片中的分组项目。 (有关所需结果enter image description here,请参见附件)。 问题是项目生成器为每个项目创建了一张卡片,但是我找不到将同一日期的所有项目放入一张卡片的方法。有什么建议吗? 我在这个主题上找到了最接近的讨论(What is the best practice to group items into CardView?),但我不知道如何将其应用于Flutter。非常感谢您的帮助!

出于说明目的,我从pub dev复制/粘贴示例代码(请参见https://pub.dev/packages/grouped_list#-example-tab-)。如官方示例所示,每个商品都有自己的卡片。这就是我被困住的地方...

import 'package:flutter/material.dart';
import 'package:grouped_list/grouped_list.dart';

void main() => runApp(MyApp());

List _elements = [
  {'name': 'John', 'group': 'Team A'},
  {'name': 'Will', 'group': 'Team B'},
  {'name': 'Beth', 'group': 'Team A'},
  {'name': 'Miranda', 'group': 'Team B'},
  {'name': 'Mike', 'group': 'Team C'},
  {'name': 'Danny', 'group': 'Team C'},
];

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Grouped List View Example'),
        ),
        body: GroupedListView<dynamic, String>(
          groupBy: (element) => element['group'],
          elements: _elements,
          sort: true,
          groupSeparatorBuilder: (String value) => Padding(
            padding: const EdgeInsets.all(8.0),
            child: Center(
                child: Text(
              value,
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            )),
          ),
          itemBuilder: (c, element) {
            return Card(
              elevation: 8.0,
              margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
              child: Container(
                child: ListTile(
                  contentPadding:
                      EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
                  leading: Icon(Icons.account_circle),
                  title: Text(element['name']),
                  trailing: Icon(Icons.arrow_forward),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

0 个答案:

没有答案