-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extendBodyBehindAppBar breaks ScrollController attachment #116120
Comments
Reproducible using the code sample in the gist shared above. Pasted below for convenience. sampleimport 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) => const MaterialApp(
home: TestScreen(),
);
}
class TestScreen extends StatefulWidget {
const TestScreen({super.key});
@override
State<TestScreen> createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> {
final controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: PreferredSize(
preferredSize: const Size.fromHeight(56),
child: AnimatedBuilder(
animation: controller,
builder: (_, __) => AppBar(
title: Text(controller.offset.toString()),
),
),
),
body: ListView.builder(
controller: controller,
itemBuilder: (context, index) => SizedBox(
height: 32,
child: Text(index.toString()),
),
),
);
}
} logs
flutter doctor -v
|
It looks like the AppBar in the sample code is expecting the position to always be attached, this is not necessarily true. The scroll controller may not have the position attached by the time the AppBar tries to access it. When In general, if you are going to access the position on a scroll controller, it is good practice to check if ScrollController.hasClients first. _BodyBuilder in Scaffold is likely where to look. It adds a LayoutBuilder if |
I wanted to animate app bar depending on scroll position (with an image behind it, that's the whole point of animation):
That's how it looks like currently (with the snippet below, but after rebuilding the widget tree with setState):
After adding
extendBodyBehindAppBar: true
to the code, on initial load (before hot reload) the screen looks like this (screenshot from my example below):Relevant snippet:
Stacktrace:
Basically adding
extendBodyBehindAppBar: true
somehow causes the ScrollController to not get attached.Reproducible example:
https://gist.github.com/eEQK/82c113b4c5471dd33961f8c07b750015
Reproducible example in dartpad:
https://dartpad.dev/?id=82c113b4c5471dd33961f8c07b750015
In order to reproduce, copy the code from the gist and run it in dartpad.dev or run it as an application
I can consistently reproduce it on:
The text was updated successfully, but these errors were encountered: