flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
161.86k stars 26.57k forks source link

AppBar shape disappears on AppBar elevation change when scrolling #145945

Closed KristijanZic closed 2 weeks ago

KristijanZic commented 1 month ago

Steps to reproduce

Following any setState (in app or via hot reloading) the AppBar shape will suddenly disappear when a SingleChildScrollView or a ListView in the same view moves from 0 or reaches 0. For the bug to happen again, setState has to be called again and the ScrollView/ListView has to move away from 0, if it was 0 initially, or reach 0 if it wasn't.

This bug only seems to be present in material 3, not material 2.

tested platforms: iOS, Android, Linux, macOS, web

affected platforms: iOS, Android, Linux, macOS, web affected channels: stable, master affected renderers: skia, impeller

Expected results

The AppBar shape should not ever disappear when set.

Actual results

AppBar shape clipper disappears when scrolling

Code sample

Code sample ```dart import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), shape: CustomAppBarShape(), ), body: SingleChildScrollView( primary: true, child: Column( children: [ for (var i = 0; i < 100; i++) ListTile( title: Text('Test $i'), leading: Text('$_counter'), ), ], ), ), // The bug happens both with SingleChildScrollView and ListView scrolling // body: ListView( // primary: true, // children: [ // for (var i = 0; i < 1000; i++) // ListTile( // title: Text('Test $i'), // leading: Text('$_counter'), // ), // ], // ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } } class CustomAppBarShape extends ContinuousRectangleBorder { @override Path getOuterPath(Rect rect, {TextDirection? textDirection}) { double height = rect.height + 80; double width = rect.width; print('SHAPE CALLED'); var path = Path(); path.lineTo(0, 0); path.lineTo(width, 0); path.lineTo(width, height + 40); path.lineTo(0, height - 100); path.close(); return path; } } ```

Screenshots or Video

Screenshots / Video demonstration https://github.com/flutter/flutter/assets/10299892/192b3a4a-9b74-46a5-b641-445f7cd8a6d0

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor output ```console [✓] Flutter (Channel master, 3.21.0-17.0.pre.19, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB) • Flutter version 3.21.0-17.0.pre.19 on channel master at /Users/kristijanzic/Library/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 11d632e5dd (40 minutes ago), 2024-03-28 19:21:34 -0400 • Engine revision b917b24836 • Dart version 3.4.0 (build 3.4.0-282.0.dev) • DevTools version 2.34.1 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) • Android SDK at /Users/kristijanzic/Library/Android/sdk • Platform android-34, build-tools 33.0.1 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) [✓] IntelliJ IDEA Community Edition (version 2023.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.84.0 [✓] Connected device (4 available) • iPhone 15 Pro (mobile) • 6C492229-B053-4C1A-ADE2-785A83F06908 • ios • com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 14.3.1 23D60 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.3.1 23D60 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 122.0.6261.57 [✓] Network resources • All expected network resources are available. • No issues found! ```
KristijanZic commented 1 month ago

I've dug some more into this and it seems that the flicker of the AppBar shape happens on AppBar elevation change when the content get's scrolled underneath the AppBar.

A workaround would be to set scrolledUnderElevation: 0.0, in the AppBar as that doesn't seem to call any state change and thus the shape doesn't flicker. Unfortunately that means that we won't be able to use the elevation feature as per Material 3 HIG.

huycozy commented 1 month ago

Thanks for the report. I'm able to reproduce the issue as well.

flutter doctor -v (stable and master) ```bash [✓] Flutter (Channel stable, 3.19.5, on macOS 14.1 23B74 darwin-x64, locale en-VN) • Flutter version 3.19.5 on channel stable at /Users/huynq/Documents/GitHub/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 300451adae (25 hours ago), 2024-03-27 21:54:07 -0500 • Engine revision e76c956498 • Dart version 3.3.3 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15C500b • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app/ • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.84.0 [✓] Connected device (3 available) • Pixel 7 (mobile) • 2B171FDH20084L • android-arm64 • Android 14 (API 34) • macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.86 [✓] Network resources • All expected network resources are available. • No issues found! ``` ```bash [!] Flutter (Channel master, 3.21.0-17.0.pre.24, on macOS 14.1 23B74 darwin-x64, locale en-VN) • Flutter version 3.21.0-17.0.pre.24 on channel master at /Users/huynq/Documents/GitHub/flutter_master ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 1a2f34ab5b (63 minutes ago), 2024-03-28 19:39:17 -0700 • Engine revision 68aa9ba386 • Dart version 3.4.0 (build 3.4.0-282.0.dev) • DevTools version 2.34.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15C500b • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app/ • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.84.0 [✓] Connected device (3 available) • Pixel 7 (mobile) • 2B171FDH20084L • android-arm64 • Android 14 (API 34) • macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.86 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ```
HansMuller commented 3 weeks ago

CC @Piinks

TahaTesser commented 3 weeks ago

Up on investigating this issue, this looks this is reproduced due to custom shape having incomplete implementation, without lerp functions or a const constructor.

This can be reproduced with just Material widget when updating elevation it rebuilds.

The custom shape without lerp functions fails to be identified by ShapeBorder.lerp as identical when the widget is rebuilt and falls back to parent shape which is ContinuousRectangleBorder in this case.

Complete custom shappe implementation should have lerp functions to identify the custom shape and lerp it's properties.

https://github.com/flutter/flutter/blob/45deb49716b4c74db48994f08ea674278aeb4564/packages/flutter/lib/src/material/material.dart#L847

expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { int _elevation = 0; void _incrementElevation() { setState(() { _elevation++; }); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData(useMaterial3: false), home: Scaffold( body: Center( child: GestureDetector( onTap: () { _incrementElevation(); }, child: Material( elevation: _elevation.toDouble(), color: Colors.red, shape: CustomShape(), child: const SizedBox( width: 100, height: 100, child: Center(child: Text('Tap me')), ), ), ), ), ), ); } } class CustomShape extends ContinuousRectangleBorder { // const CustomShape(); @override Path getOuterPath(Rect rect, {TextDirection? textDirection}) { double height = rect.height + 80; double width = rect.width; // print('SHAPE CALLED'); var path = Path(); path.lineTo(0, 0); path.lineTo(width, 0); path.lineTo(width, height + 40); path.lineTo(0, height - 100); path.close(); return path; } // @override // ShapeBorder? lerpFrom(ShapeBorder? a, double t) { // if (a is CustomShape) { // return CustomShape(); // } // return super.lerpFrom(a, t); // } // @override // ShapeBorder? lerpTo(ShapeBorder? b, double t) { // if (b is CustomShape) { // return CustomShape(); // } // return super.lerpTo(b, t); // } } ```

To fix this please implement lerp functions.

class CustomAppBarShape extends ContinuousRectangleBorder {
  const CustomAppBarShape({super.borderRadius = BorderRadius.zero});

  @override
  Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
    double height = rect.height + 80;
    double width = rect.width;

    print('SHAPE CALLED');

    var path = Path();
    path.lineTo(0, 0);
    path.lineTo(width, 0);
    path.lineTo(width, height + 40);
    path.lineTo(0, height - 100);
    path.close();
    return path;
  }

  @override
  ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
    if (a is CustomAppBarShape) {
      return CustomAppBarShape(borderRadius: BorderRadiusGeometry.lerp(a.borderRadius, borderRadius, t)!);
    }
    return super.lerpFrom(a, t);
  }

  @override
  ShapeBorder? lerpTo(ShapeBorder? b, double t) {
    if (b is CustomAppBarShape) {
      return CustomAppBarShape(borderRadius: BorderRadiusGeometry.lerp(b.borderRadius, borderRadius, t)!);
    }
    return super.lerpTo(b, t);
  }
}

Fixed code sample for AppBar

expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), shape: const CustomAppBarShape(), ), body: SingleChildScrollView( primary: true, child: Column( children: [ for (var i = 0; i < 100; i++) ListTile( title: Text('Test $i'), leading: Text('$_counter'), ), ], ), ), // The bug happens both with SingleChildScrollView and ListView scrolling // body: ListView( // primary: true, // children: [ // for (var i = 0; i < 1000; i++) // ListTile( // title: Text('Test $i'), // leading: Text('$_counter'), // ), // ], // ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } } class CustomAppBarShape extends ContinuousRectangleBorder { const CustomAppBarShape({super.borderRadius = BorderRadius.zero}); @override Path getOuterPath(Rect rect, {TextDirection? textDirection}) { double height = rect.height + 80; double width = rect.width; print('SHAPE CALLED'); var path = Path(); path.lineTo(0, 0); path.lineTo(width, 0); path.lineTo(width, height + 40); path.lineTo(0, height - 100); path.close(); return path; } @override ShapeBorder? lerpFrom(ShapeBorder? a, double t) { if (a is CustomAppBarShape) { return CustomAppBarShape(borderRadius: BorderRadiusGeometry.lerp(a.borderRadius, borderRadius, t)!); } return super.lerpFrom(a, t); } @override ShapeBorder? lerpTo(ShapeBorder? b, double t) { if (b is CustomAppBarShape) { return CustomAppBarShape(borderRadius: BorderRadiusGeometry.lerp(b.borderRadius, borderRadius, t)!); } return super.lerpTo(b, t); } } ```

Preview

This is similar to other shapes in the framework that do not reproduce this issue.

https://github.com/flutter/flutter/blob/45deb49716b4c74db48994f08ea674278aeb4564/packages/flutter/lib/src/painting/stadium_border.dart#L26-L37

TahaTesser commented 3 weeks ago

@HansMuller This seems to be working as expected.

Should we add an example for using custom shapes with Material or Material contained widgets such as AppBar?

HansMuller commented 3 weeks ago

I would never say no to adding an example. In this case I think it would be particularly worthwhile!

KristijanZic commented 3 weeks ago

I can confirm that this was in fact...a skill issue and it works perfectly fine with Taha's example. That said no linter has complained about the original example.

TahaTesser commented 3 weeks ago

That said no linter has complained about the original example.

These are optional methods. The constructor alone would've fixed the issue, as it allows identical method to properly compare custom shape objects. My new example include a comment for this and lerp functions, hopefully this will make it clear.

github-actions[bot] commented 6 days ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.