build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  print("ReportMyListView build: ${Report.unviewedRef.path}");
  // To have a emptybuilder need to be FirebaseDatabaseQueryBuilder
  return FirebaseDatabaseQueryBuilder(
      query: Report.unviewedRef.orderByChild('uid').equalTo(myUid!),
      builder: (context, snapshot, index) {
        if (snapshot.hasError) {
          return errorBuilder?.call(
                  context, snapshot.error, snapshot.stackTrace) ??
              Text("report error: ${snapshot.error}");
        }
        if (snapshot.isFetching) {
          return loadingBuilder?.call(context) ??
              const Center(child: CircularProgressIndicator());
        }

        if (snapshot.docs.isEmpty) {
          return emptyBuilder?.call(context) ??
              const Center(child: Text("No reports"));
        }
        return ListView.separated(
            padding: padding ?? const EdgeInsets.all(8),
            separatorBuilder: (_, index) =>
                separatorBuilder?.call(_, index) ?? const SizedBox.shrink(),
            itemCount: snapshot.docs.length,
            itemBuilder: (context, index) {
              if (snapshot.hasMore && index + 1 == snapshot.docs.length) {
                // Tell FirestoreQueryBuilder to try to obtain more items.
                // It is safe to call this function from within the build method.
                snapshot.fetchMore();
                dog("fetching more users");
              }
              final Report report = Report.fromValue(
                  snapshot.docs[index].value, snapshot.docs[index].ref);

              func() async {
                final re = await confirm(
                    context: context,
                    title: 'Delete report',
                    message: 'Do you want to delete this report?');
                if (re != true) return;
                await report.ref.remove();
              }

              if (report.isPost) {
                return FutureBuilder<Post?>(
                  future: Post.get(
                    category: report.category!,
                    id: report.postId!,
                  ),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return const Center(
                          child: CircularProgressIndicator.adaptive());
                    }
                    final post = snapshot.data!;
                    return postBuilder?.call(report, post, func) ??
                        ListTile(
                          leading: UserAvatar(uid: post.uid),
                          title: Text(report.reason),
                          subtitle: Row(
                            children: [
                              const Text('[POST] '),
                              UserDoc.field(
                                uid: post.uid,
                                field: 'displayName',
                                builder: (name) => Text(name ?? ''),
                              ),
                              Text(' ${report.createdAt.toShortDate}'),
                            ],
                          ),
                          trailing: IconButton(
                            key: Key('reportUser${report.otherUserUid}'),
                            onPressed: func,
                            icon: const Icon(Icons.delete),
                          ),
                        );
                  },
                );
              } else if (report.isComment) {
                return FutureBuilder<Comment?>(
                  future: Comment.get(
                    postId: report.postId!,
                    commentId: report.commentId!,
                  ),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return const Center(
                          child: CircularProgressIndicator.adaptive());
                    }
                    final comment = snapshot.data!;
                    return commentBuilder?.call(report, comment, func) ??
                        ListTile(
                          leading: UserAvatar(uid: comment.uid),
                          title: Text(report.reason),
                          subtitle: Row(
                            children: [
                              const Text('[COMMNET] '),
                              UserDoc.field(
                                uid: comment.uid,
                                field: 'displayName',
                                builder: (name) => Text(name ?? ''),
                              ),
                              Text(' ${report.createdAt.toShortDate}'),
                            ],
                          ),
                          trailing: IconButton(
                            key: Key('reportUser${report.otherUserUid}'),
                            onPressed: func,
                            icon: const Icon(Icons.delete),
                          ),
                        );
                  },
                );
              } else if (report.isChatRoom) {
                dog('${report.chatRoomId} ${report.otherUserUid} ');

                final room = ChatRoom.fromUid(report.otherUserUid!);
                return chatBuilder?.call(report, func) ??
                    ListTile(
                      title: Text(report.reason),
                      subtitle: Row(
                        children: [
                          if (room.isSingleChat) ...{
                            const Text('[CHAT] '),
                            Value.once(
                                // path: ChatRoom.chatRoomName(report.chatRoomId),
                                ref: ChatJoin.nameRef(report.chatRoomId!),
                                builder: (v) {
                                  // no name is null on chat-rooms/chatid/name
                                  // Explanation: Name is null because chat room can be a single
                                  //              Chat Room, which doesn't usually have Chat
                                  //              Room Name.
                                  return Text(v ?? 'No Chat Room Name');
                                }),
                          } else ...{
                            const Text('[GROUP CHAT] '),
                            Value.once(
                                // path: ChatRoom.chatRoomName(report.chatRoomId),
                                ref: ChatRoom.nameRef(report.chatRoomId!),
                                builder: (v) {
                                  // no name is null on chat-rooms/chatid/name
                                  // Explanation: Name is null because chat room can be a single
                                  //              Chat Room, which doesn't usually have Chat
                                  //              Room Name.
                                  return Text(v ?? 'No Chat Room Name');
                                }),
                          },
                          Text(' ${report.createdAt.toShortDate}'),
                        ],
                      ),
                      trailing: IconButton(
                        key: Key('reportUser${report.otherUserUid}'),
                        onPressed: func,
                        icon: const Icon(Icons.delete),
                      ),
                    );
              } else {
                return userBuilder?.call(report, func) ??
                    ListTile(
                      leading: UserAvatar(
                        uid: report.otherUserUid!,
                      ),
                      title: Text(report.reason),
                      subtitle: Row(
                        children: [
                          UserDisplayName(uid: report.otherUserUid!),
                          Text(' ${report.createdAt.toShortDate}'),
                        ],
                      ),
                      trailing: IconButton(
                        key: Key('reportUser${report.otherUserUid}'),
                        onPressed: func,
                        icon: const Icon(Icons.delete),
                      ),
                    );
              }
            });
      });
}