dart 当我已经使用ListTile模拟2种不同的消息时,如何添加第3种消息类型?

vbopmzt1  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(103)

我目前正在使用ListTile来模拟在群聊中发送和接收的消息。这工作得很好,但现在我需要添加包含第三种消息类型的功能,我不知道是否可以使用ListTile这样做,或者是否需要使用RowsContainers从头开始重做。
现在,该条件只是检查uid == currentUID,以确定是在左侧还是右侧显示消息。我需要更改此设置以检查消息的类型,如果是1类型,则在中间显示聊天消息。否则,对uid == currentUID执行常规检查,以确定它应该在左侧还是右侧。

@override
  Widget build(BuildContext context) {
    return Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: chats.length,
              itemBuilder: (context, index) {
                final isSelected = index == selectedIndex;
                final msg = ModelChatData.fromJson(chats[index]);
                final text = msg.text;
                final timestamp = msg.timestamp;
                final type = msg.type;
                var uid = '';
                var username = '';

                if (type == 0) { // regular message
                  uid = msg.uid!;
                  username = msg.username!;
                }

                if (type == 1) {
                   //do something that will allow the code below to show the chat message in the middle of the screen
                }

                return FractionallySizedBox(
                  widthFactor: 1, // set width of listtile
                  child: ListTile(
                    onTap: () {
                      setState(() {
                        selectedIndex = isSelected ? null : index;
                      });
                    },
                    leading: uid == currentUID
                        ? const SizedBox.shrink()
                        : const CircleAvatar(child: FlutterLogo(),),
                    title: Column(
                      crossAxisAlignment: uid == currentUID
                          ? CrossAxisAlignment.end
                          : CrossAxisAlignment.start,
                      children: [
                        Text(
                          username,
                          style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                        ),
                      ],
                    ),
                    trailing: uid == currentUID
                        ? const CircleAvatar(child: FlutterLogo(),)
                        : const SizedBox.shrink(),
                    subtitle: Column(
                      crossAxisAlignment: uid == currentUID
                          ? CrossAxisAlignment.end
                          : CrossAxisAlignment.start,
                      children: [
                        Text(text),
                        Visibility(
                          visible:
                          isSelected, // used for show/hide date when message is clicked on
                          child: Text(
                            DateTime.now().toString(),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      );
  }
ca1c2owp

ca1c2owp1#

你需要添加条件来实现这一点.....

....
type == 1 ?
             FractionallySizedBox(
                      widthFactor: 1, // set width of listtile
                      child: ListTile(
                        onTap: () {
                          setState(() {
                            selectedIndex = isSelected ? null : index;
                          });
                        },
                        title: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            const CircleAvatar(
                              child: FlutterLogo(),
                            ),
                            SizedBox(width: 10),
                            Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  'Username ${index + 1}',
                                  style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16),
                                ),
                                Text('This is message ${index + 1}'),
                                Visibility(
                                  visible:
                                      isSelected, // used for show/hide date when message is clicked on
                                  child: Text(
                                    DateTime.now().toString(),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    )
       :  FractionallySizedBox(
                  widthFactor: 1, // set width of listtile
                  child: ListTile(
                    onTap: () {
                      setState(() {
                        selectedIndex = isSelected ? null : index;
                      });
                    },
                    leading: uid == currentUID
                        ? const SizedBox.shrink()
                        : const CircleAvatar(child: FlutterLogo(),),
                    title: Column(
                      crossAxisAlignment: uid == currentUID
                          ? CrossAxisAlignment.end
                          : CrossAxisAlignment.start,
                      children: [
                        Text(
                          username,
                          style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                        ),
                      ],
                    ),
                    trailing: uid == currentUID
                        ? const CircleAvatar(child: FlutterLogo(),)
                        : const SizedBox.shrink(),
                    subtitle: Column(
                      crossAxisAlignment: uid == currentUID
                          ? CrossAxisAlignment.end
                          : CrossAxisAlignment.start,
                      children: [
                        Text(text),
                        Visibility(
                          visible:
                          isSelected, // used for show/hide date when message is clicked on
                          child: Text(
                            DateTime.now().toString(),
                          ),
                        ),
                      ],
                    ),
                  ),
),

您的ListView.builder返回上面的小部件。

相关问题