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.72k stars 27.62k forks source link

Wrong text height calculation when Text inside WidgetSpan #146407

Open karabanovbs opened 7 months ago

karabanovbs commented 7 months ago

Steps to reproduce

1) Create Rich text widget; 2) Put WidgetSpan inside rich text; 3) Put Text (text should wrap the line) widget inside Widget span 4) Put TextSpan with line break after WidgetSpan

May be it is important: Without step No. 4 or without line wrapping inside Text it works good.

Expected results

Expected result image

Actual results

Unexpected space

image

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 const MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( body: Center( child: ColoredBox( color: Colors.red, child: SizedBox( width: 300, child: Text.rich( TextSpan( children: [ WidgetSpan( child: Text( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', ), ), TextSpan(text: '\n'), ], ), ), ), ), ), ), ); } } ```

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output ```console [✓] Flutter (Channel stable, 3.19.4, on macOS 14.1.1 23B81 darwin-arm64 (Rosetta), locale en-AE) • Flutter version 3.19.4 on channel stable at /Users/boriskarabanov/fvm/versions/3.19.4 • Upstream repository https://github.com/flutter/flutter.git • Framework revision 68bfaea224 (3 weeks ago), 2024-03-20 15:36:31 -0700 • Engine revision a5c24f538d • Dart version 3.3.2 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/boriskarabanov/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/boriskarabanov/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • 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.14.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/boriskarabanov/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.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/boriskarabanov/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 76.3.4 • Dart plugin version 232.10248 [✓] VS Code (version 1.88.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.86.0 [✓] Connected device (3 available) • SM S9010 (mobile) • R5CT444XHFN • android-arm64 • Android 14 (API 34) • macOS (desktop) • macos • darwin-arm64 • macOS 14.1.1 23B81 darwin-arm64 (Rosetta) • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.107 ! Error: Browsing on the local area network for Boris Karabanov’s 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. • No issues found! ```
danagbemava-nc commented 7 months ago

Reproducible using the code sample provided above.

flutter doctor -v ``` [!] Flutter (Channel stable, 3.19.5, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB) • Flutter version 3.19.5 on channel stable at /Users/nexus/dev/sdks/flutter ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutters/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutter. Consider adding /Users/nexus/dev/sdks/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutters/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutter. Consider adding /Users/nexus/dev/sdks/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 300451adae (11 days ago), 2024-03-27 21:54:07 -0500 • Engine revision e76c956498 • Dart version 3.3.3 • DevTools version 2.31.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/nexus/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer • Build 15E204a • CocoaPods version 1.14.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/nexus/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.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 77.2.2 • Dart plugin version 232.10286 [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.86.0 [✓] Connected device (4 available) • Nexus (mobile) • 00008020-001875E83A38002E • ios • iOS 17.4.1 21E236 • Dean’s iPad (mobile) • 00008103-000825C811E3401E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-arm64 • macOS 14.3.1 23D60 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.107 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ``` ``` [✓] Flutter (Channel master, 3.22.0-5.0.pre.55, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB) • Flutter version 3.22.0-5.0.pre.55 on channel master at /Users/nexus/dev/sdks/flutters • Upstream repository https://github.com/flutter/flutter.git • Framework revision ddf79b755d (12 hours ago), 2024-04-07 18:34:24 -0400 • Engine revision c10e950c66 • Dart version 3.5.0 (build 3.5.0-18.0.dev) • DevTools version 2.34.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer • Build 15E204a • CocoaPods version 1.14.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/nexus/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.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 77.2.2 • Dart plugin version 232.10286 [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.86.0 [✓] Connected device (5 available) • Nexus (mobile) • 00008020-001875E83A38002E • ios • iOS 17.4.1 21E236 • Dean’s iPad (mobile) • 00008103-000825C811E3401E • ios • iOS 17.4.1 21E236 • 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 123.0.6312.107 [✓] Network resources • All expected network resources are available. • No issues found! ```
asci-00 commented 6 months ago

This seems to be a problem that occurs because when a new line occurs, the height of the new line is set to the height of the previous text (object?).

         ColoredBox(
            color: Colors.red,
            child: SizedBox(
              width: 300,
              child: Text.rich(
                TextSpan(
                  children: [
                    WidgetSpan(
                      child: Container(
                        color: Colors.blue,
                        height: 100.0,
                      ),
                    ),
                    TextSpan(text: '\n'),
                  ],
                ),
              ),
            ),
          ),

image

When the height of the new line is unknown, it seems that the height of the new line is determined based on the height of the previous text(?)

In fact, if the text is inserted after a newline (so they know the new line height), it displays well.

         ColoredBox(
            color: Colors.red,
            child: SizedBox(
              width: 300,
              child: Text.rich(
                TextSpan(
                  children: [
                    WidgetSpan(
                      child: Container(
                        color: Colors.blue,
                        height: 100.0,
                      ),
                    ),
                    TextSpan(text: '\n '),
                  ],
                ),
              ),
            ),
          ),

image

I'm not sure because I haven't seen the implementation part in detail, but it seems to be managed by text_painter. At first glance, it seems like a pretty natural movement..

amake commented 1 month ago

At first glance, it seems like a pretty natural movement..

I disagree that the current behavior is natural. If you place a \n after the WidgetSpan the second line will be extremely tall. If you place a \n\u200c (U+200C ZERO WIDTH NON-JOINER—a non-printing character) it will be normal. Why would a non-printing character affect the height of the line?