Skip to content

[Impeller]TextPainter with gradient has anti-aliasing problem. #132928

@lucian1024

Description

@lucian1024

Is there an existing issue for this?

Steps to reproduce

I wrote a GradientText widget which uses TextPainter. It works well with Flutter 3.7.1. But after I upgrage to Flutter 3.10.6, it has anti-aliasing problem on iOS.
And if I disable Impeller with command flutter run --no-enable-impeller, it works well too.

Expected results

image

Actual results

image

Code sample

Code sample
Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GradientText(
          text: [
            GradientTextData(
              text: "abcdefg",
              gradient: LinearGradient(
                colors: [const Color(0xFF70EAAF), const Color(0xFF2A9963)],
                begin: AlignmentDirectional.centerStart,
                end: AlignmentDirectional.centerEnd,
              ),
              style: TextStyle(
                color: Colors.white,
                fontSize: 21,
                fontWeight: FontWeight.w600,
                overflow: TextOverflow.ellipsis,
              ),
            )
          ],
        ),
      )
    );
  }

class GradientText extends StatelessWidget {
  const GradientText({
    required this.text,
    this.maxLines,
    this.ellipsis,
    super.key,
  });

  final List<GradientTextData> text;
  final int? maxLines;
  final String? ellipsis;

  @override
  Widget build(BuildContext context) {
    final TextPainter textPainter = _GradientTextPainter.createTextPainter(
      text,
      maxLines: maxLines,
      ellipsis: ellipsis,
    );
    textPainter.layout();

    return CustomPaint(
      size: textPainter.size,
      painter: _GradientTextPainter(
        text: text,
        maxLines: maxLines,
        ellipsis: ellipsis,
      ),
    );
  }
}

@immutable
class GradientTextData {
  const GradientTextData({
    required this.text,
    required this.gradient,
    required this.style,
  });

  final String text;
  final Gradient gradient;
  final TextStyle style;
}

class _GradientTextPainter extends CustomPainter {
  _GradientTextPainter({
    required this.text,
    this.maxLines,
    this.ellipsis,
  });

  final List<GradientTextData> text;
  final int? maxLines;
  final String? ellipsis;

  static TextPainter createTextPainter(
    List<GradientTextData> text, {
    Size? gradientSize,
    int? maxLines,
    String? ellipsis,
  }) {
    return TextPainter(
      maxLines: maxLines,
      ellipsis: ellipsis,
      text: TextSpan(
        children: text.map((e) {
          TextStyle textStyle = e.style;
          if (gradientSize != null) {
            final Paint gradientPaint = new Paint()
              ..shader = e.gradient.createShader(
                new Rect.fromLTWH(0.0, 0.0, gradientSize.width, gradientSize.height),
                textDirection: TextDirection.ltr,
              );
            textStyle = textStyle.copyWith(foreground: gradientPaint);
          }

          return TextSpan(
            text: e.text,
            style: textStyle,
          );
        }).toList(),
      ),
      textDirection: TextDirection.ltr,
    );
  }

  @override
  void paint(Canvas canvas, Size size) {
    TextPainter textPainter = createTextPainter(
      text,
      gradientSize: size,
      maxLines: maxLines,
      ellipsis: ellipsis,
    );
    textPainter.layout(minWidth: 0, maxWidth: size.width,);
    textPainter.paint(canvas, Offset.zero);
  }

  @override
  bool shouldRepaint(_GradientTextPainter oldDelegate) {
    return text != oldDelegate.text;
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.5 22G74 darwin-x64, locale zh-Hans-CN)
    • Flutter version 3.10.6 on channel stable at /Users/liuxin/fvm/versions/3.10.6
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (6 weeks ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1
    • 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 33.0.0)
    • Android SDK at /Users/liuxin/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • ANDROID_HOME = /Users/liuxin/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.12.1

[✓] Android Studio (version 2022.3)
    • 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.6+0-17.0.6b829.9-10027231)

[✓] IntelliJ IDEA Community Edition (version 2022.2.3)
    • 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.81.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.70.0

[✓] Connected device (2 available)
    • HMA AL00 (mobile)      • 66J5T19411000579                     • android-arm64 • Android 10 (API 29)
    • iPhone 14 Pro (mobile) • BE017752-6B45-48AF-A051-68E3255CD227 • ios           • com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)

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

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work liste: impellerImpeller rendering backend issues and features requestsengineflutter/engine related. See also e: labels.found in release: 3.13Found to occur in 3.13found in release: 3.14Found to occur in 3.14has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-engineOwned by Engine teamtriaged-engineTriaged by Engine teamwaiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions