Search code examples
flutterlistviewanimationoverlap

Flutter ListView scroll animation overlapping sibling widget


Hoping someone can help with this and it's not a bug and it's just me being silly.

There is very strange behavior from listview when it's not taking the full length of the screen and in a column.

When you scroll down, the animation at max extent persists and overlaps. I'm assuming this is a bug and not by design.

Here's the simple code to reproduce.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp(
    items: List<MessageItem>.generate(
      33,
      (i) => MessageItem("Sender $i", "Message body $i"),
    ),
  ));
}

class MyApp extends StatelessWidget {
  final List<MessageItem> items;

  MyApp({Key key, @required this.items}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final title = 'Mixed List';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Column(
          children: [
            Expanded(
              child: Container(),
            ),
            Expanded(
              child: ListView.builder(
                itemCount: items.length,
                itemBuilder: (context, index) {
                  final item = items[index];

                  return ListTile(
                    title: item.buildTitle(context),
                    subtitle: item.buildSubtitle(context),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/// A ListItem that contains data to display a message.
class MessageItem {
  final String sender;
  final String body;

  MessageItem(this.sender, this.body);

  Widget buildTitle(BuildContext context) => Text(sender);

  Widget buildSubtitle(BuildContext context) => Text(body);
}

bug


Solution

  • So final code will be. I have added the scroll phisycs BouncingScrollPhysics.

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp(
        items: List<MessageItem>.generate(
          33,
          (i) => MessageItem("Sender $i", "Message body $i"),
        ),
      ));
    }
    
    class MyApp extends StatelessWidget {
      final List<MessageItem> items;
    
      MyApp({Key key, @required this.items}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        final title = 'Mixed List';
    
        return MaterialApp(
          title: title,
          home: Scaffold(
            appBar: AppBar(
              title: Text(title),
            ),
            body: Column(
              children: [
                Expanded(
                  child: Container(
                  ),
                ),
                Expanded(
                  child: ListView.builder(
                    physics: BouncingScrollPhysics(
                        parent: AlwaysScrollableScrollPhysics()),
                    itemCount: 50,
                    itemBuilder: (context, index) {
                      return ListTile(
                        title: Text("${index + 1}"),
                        subtitle: Text("${index + 1}"),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    /// A ListItem that contains data to display a message.
    class MessageItem {
      final String sender;
      final String body;
    
      MessageItem(this.sender, this.body);
    
      Widget buildTitle(BuildContext context) => Text(sender);
    
      Widget buildSubtitle(BuildContext context) => Text(body);
    }