Single card is not single

Issue

I’m trying to make posts like on Twitter, where every post is a single separated item, but they all go like big single card, without separating. I’m kinda new with Flutter and bad at explanations, but I hope u got it.

 @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: _writePost,
          tooltip: 'Increment',
          child: Icon(Icons.create, color: Colors.grey[300]),
        ),
        body: SizedBox(
          height: 500, 
          child:
              StreamBuilder<List<Post>>(
            initialData: const [],
            stream: _socketStream.stream,
            builder: (context, snapshot) {
              if (_isLoading) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }
              return Card(child: ListView(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                children: [
                  ...snapshot.data!.map<Widget>(
                    (post) => Padding(
                      key: ValueKey(post.id),
                      padding: const EdgeInsets.symmetric(vertical: 10),
                      child: ListTile(
                        title: Text(
                          post.content,
                          style: const TextStyle(fontSize: 20),
                        ),
                        trailing: MaterialButton(
                          onPressed: () {
                            _deletePost(post.id);
                          },
                          child: const Icon(
                            Icons.delete,
                            size: 30,
                          ),
                        ),
                      ),
                    ),
                  )
                ],
               ) );
            },
          ),
        ));
  }

Example of how it looks:
enter image description here

I’m tried to find error with documentation, but…
Write if u need some more code or explanations.
Please help me if you can <3

Solution

You need to add each of the mapped item of your data with Card instead of your parent ListView. Something like this:

return ListView(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            children: [
              ...snapshot.data!.map<Widget>(
                (post) => Padding(
                  key: ValueKey(post.id),
                  padding: const EdgeInsets.symmetric(vertical: 10),
                  child:
                   // Use card here.
                   Card(child: ListTile(
                    title: Text(
                      post.content,
                      style: const TextStyle(fontSize: 20),
                    ),
                    trailing: MaterialButton(
                      onPressed: () {
                        _deletePost(post.id);
                      },
                      child: const Icon(
                        Icons.delete,
                        size: 30,
                      ),
                    ),
                  ),
                 ),
                ),
              )
            ],
            );

Answered By – ישו אוהב אותך

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *