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
166.4k stars 27.57k forks source link

Canvas.drawShadow does not draw a shadow for a "line" path #104171

Open abhaysood opened 2 years ago

abhaysood commented 2 years ago

Steps to Reproduce

Checkout the code below where I create a custom painter "MyPainter". The painter draws a line from left to right of the screen and adds a shadow along the same path using canvas.drawShadow. Note that the shadow doesn't appear.

Expected: A red color shadow should appear below the line. Actual: No shadow appears.

Additionally, I tried this same code by creating a path from a rect in which case the shadow appears.

Code sample ```dart import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData.light(), debugShowCheckedModeBanner: false, home: Scaffold( body: MyWidget(), ), ); } } class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: Container( color: const Color.fromARGB(255, 198, 198, 198), child: const CustomPaint( size: Size(double.infinity, 2), painter: MyPainter(), ), ), ); } } class MyPainter extends CustomPainter { const MyPainter(); @override void paint(Canvas canvas, Size size) { final paint = Paint() ..color = Colors.black ..style = PaintingStyle.stroke ..strokeWidth = 4; final path = Path()..lineTo(size.width, 0); canvas.drawPath(path, paint); canvas.drawShadow(path, Colors.red, 2, false); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) => false; } ```
Flutter doctor ``` Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.10.4, on macOS 11.6 20G165 darwin-x64, locale en-IN) [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.2) [✓] Android Studio (version 2020.3) [✓] IntelliJ IDEA Ultimate Edition (version 2021.1.2) [✓] IntelliJ IDEA Ultimate Edition (version 2021.2.3) [✓] IntelliJ IDEA Ultimate Edition (version 2021.2.3) [✓] VS Code (version 1.63.2) [✓] Connected device (1 available) [✓] HTTP Host Availability • No issues found ```
danagbemava-nc commented 2 years ago

Issue is reproducible on stable and master.

In the code sample below, MyPainter2 uses Path..addArc, which draws the shadow but MyPainter which uses Path..lineTo does not draw the shadow. Switch between the two classes to see the difference.

This reproduces on all platforms.

screenshot
code sample ```dart import 'dart:math' as math; import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData.light(), debugShowCheckedModeBanner: false, home: const Scaffold( body: MyWidget(), ), ); } } class MyWidget extends StatelessWidget { const MyWidget({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Center( child: Container( color: const Color.fromARGB(255, 198, 198, 198), child: const CustomPaint( size: Size(double.infinity, 2), // painter: MyPainter(), painter: MyPainter2(), ), ), ); } } class MyPainter extends CustomPainter { const MyPainter(); @override void paint(Canvas canvas, Size size) { final paint = Paint() ..color = Colors.black ..style = PaintingStyle.stroke ..strokeWidth = 10; final path = Path()..lineTo(size.width, 0); canvas.drawPath(path, paint); canvas.drawShadow(path, Colors.red, 5, true); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) => false; } class MyPainter2 extends CustomPainter { const MyPainter2(); @override void paint(Canvas canvas, Size size) { final rect = Rect.fromLTWH(30, 50, 50, 50); final paint = Paint() ..style = PaintingStyle.stroke ..color = Colors.red ..strokeWidth = 3; final path = Path()..addArc(rect, -math.pi / 2, math.pi); canvas.drawPath(path, paint); canvas.drawShadow(path, Colors.grey, 5, false); } @override bool shouldRepaint(CustomPainter old) { return false; } } ```
flutter doctor -v ``` [✓] Flutter (Channel stable, 3.0.0, on macOS 12.3.1 21E258 darwin-arm, locale en-GB) • Flutter version 3.0.0 at /Users/nexus/dev/sdks/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision ee4e09cce0 (9 days ago), 2022-05-09 16:45:18 -0700 • Engine revision d1b9a6938a • Dart version 2.17.0 • DevTools version 2.12.2 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-32, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.1) • 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 11.0.11+0-b60-7772763) [✓] VS Code (version 1.67.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.40.0 [✓] Connected device (3 available) • iPhone 13 (mobile) • DBB48503-41CE-4969-9043-3261726E438A • ios • com.apple.CoreSimulator.SimRuntime.iOS-15-4 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 12.3.1 21E258 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 101.0.4951.64 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ``` ``` [✓] Flutter (Channel master, 3.1.0-0.0.pre.799, on macOS 12.3.1 21E258 darwin-arm, locale en-GB) • Flutter version 3.1.0-0.0.pre.799 at /Users/nexus/dev/sdks/flutters • Upstream repository https://github.com/flutter/flutter.git • Framework revision 348a2b4f2f (5 hours ago), 2022-05-19 00:33:08 -0400 • Engine revision 1965c92ea4 • Dart version 2.18.0 (build 2.18.0-130.0.dev) • DevTools version 2.13.1 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-32, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.1) • 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 11.0.11+0-b60-7772763) [✓] VS Code (version 1.67.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.40.0 [✓] Connected device (3 available) • iPhone 13 (mobile) • DBB48503-41CE-4969-9043-3261726E438A • ios • com.apple.CoreSimulator.SimRuntime.iOS-15-4 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 12.3.1 21E258 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 101.0.4951.64 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ```