Skip to content

isAntiAlias=false doesn't work properly #168589

Closed
@Ormosia-ply

Description

@Ormosia-ply

Steps to reproduce

  1. Draw a path on canvas with antialiasing disabled and save it as an image
  2. Displaying images using CustomPaint widget

Expected results

No pixel tweening occurs

Actual results

Disabling antialiasing fails on iOS

Code sample

Code sample
import 'package:flutter/material.dart';
import 'dart:ui' as ui;

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<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _image = ValueNotifier<ui.Image?>(null);
  final size = Size(300, 300);

  @override
  void dispose() {
    _image.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          InteractiveViewer(
              maxScale: 100,
              child: CustomPaint(
                size: size,
                painter: _Painter(),
              )),
          ElevatedButton(onPressed: saveImage, child: Text('GOGOGO')),
          ValueListenableBuilder(
            valueListenable: _image,
            builder: (context, value, child) {
              return InteractiveViewer(
                  maxScale: 100,
                  child: CustomPaint(
                    size: size,
                    painter: _PreviewPainter(image: value),
                  ));
            },
          ),
        ],
      ),
    );
  }

  Future<void> saveImage() async {
    ui.PictureRecorder recorder = ui.PictureRecorder();
    Canvas canvas = Canvas(recorder);

    ui.Paint paint = ui.Paint()..color = Colors.pinkAccent;

    canvas.drawPaint(paint..color = Colors.pinkAccent);

    final path = Path()
      ..moveTo(0, 0)
      ..relativeLineTo(size.width, size.height);
    canvas.drawPath(
        path,
        paint
          ..color = Colors.blueAccent
          ..style = PaintingStyle.stroke
          ..isAntiAlias = false
          ..strokeWidth = 30);

    _image.value = await recorder
        .endRecording()
        .toImage(size.width.floor(), size.height.floor());
  }
}

class _PreviewPainter extends CustomPainter {
  final ui.Image? image;
  _PreviewPainter({this.image});

  @override
  void paint(Canvas canvas, Size size) {
    if (image != null) {
      canvas.drawImage(image!, Offset.zero, Paint());
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}

class _Painter extends CustomPainter {
  final _paint = Paint()..color = Colors.pinkAccent;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.clipRect(Offset.zero & size);
    // canvas.drawRect(Offset.zero & size, _paint..color = Colors.pinkAccent);
    canvas.drawPaint(_paint..color = Colors.pinkAccent);

    final path = Path()
      ..moveTo(0, 0)
      ..relativeLineTo(size.width, size.height);
    canvas.drawPath(
        path,
        _paint
          ..color = Colors.blueAccent
          ..style = PaintingStyle.stroke
          ..isAntiAlias = false
          ..strokeWidth = 30);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}

Screenshots or Video

Screenshots / Video demonstration

Android

Image

iOS
Image

Hopefully the effect on iOS will be the same as on Android.

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.27.1, on macOS 15.3.1 24D70 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.27.1 on channel stable at /Users/cqtongji/Documents/sdks/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (5 months ago), 2024-12-17 03:23:09 +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/cqtongji/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Android Studio (version 2024.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 21.0.3+-79915917-b509.11)

[✓] VS Code (version 1.99.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.110.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554                        • android-arm64 • Android 12 (API 31) (emulator)
    • iPhone 16 Plus (mobile)         • 95ABC060-2502-4C30-ABD0-024D3CF0B49C • ios           • com.apple.CoreSimulator.SimRuntime.iOS-18-1
      (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64  • macOS 15.3.1 24D70 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin        • macOS 15.3.1 24D70 darwin-arm64
    ! Error: Browsing on the local area network for MingyangのiPhone. Ensure the device is unlocked and attached with a cable or associated
      with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: renderingUI glitches reported at the engine/skia or impeller rendering levelengineflutter/engine repository. See also e: labels.found in release: 3.29Found to occur in 3.29found in release: 3.32Found to occur in 3.32has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: duplicateIssue is closed as a duplicate of an existing issueteam-engineOwned by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions