Skip to content

Conversation

@Dhruv1797
Copy link
Contributor

@Dhruv1797 Dhruv1797 commented Nov 18, 2025

Fixes: #284
Add a dynamic Text Editor where text always fits inside a fixed canvas by auto-resizing as content grows. On finalize, the text should export as an image similar to other elements.

Here is the demo video of this feature:

textfit_editor.mp4

Summary by Sourcery

Add a dynamic text editor that auto-resizes content to fit a fixed canvas and exports the final text as an image

New Features:

  • Introduce TextFitEditor component for entering and auto-scaling text to fill a predefined canvas
  • Add a Text action button in the BottomActionMenu to launch the text editor and insert the rendered text as an image

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Nov 18, 2025

Reviewer's Guide

Adds a new TextFitEditor widget for dynamic, auto-resizing text editing and integrates it into the image editor's BottomActionMenu, enabling users to insert text that always fits the canvas and export it as an image.

Sequence diagram for adding text via TextFitEditor

sequenceDiagram
    actor User
    participant "BottomActionMenu"
    participant "TextFitEditor"
    participant "imgLoader"
    User->>"BottomActionMenu": Tap 'Text' button
    "BottomActionMenu"->>"TextFitEditor": Open editor with canvas size
    User->>"TextFitEditor": Enter text, select options
    "TextFitEditor"->>"BottomActionMenu": Return exported image bytes
    "BottomActionMenu"->>"imgLoader": updateImage(bytes)
    "BottomActionMenu"->>"imgLoader": saveFinalizedImageBytes(bytes)
    "BottomActionMenu"->>User: Show updated canvas
Loading

Class diagram for new TextFitEditor widget

classDiagram
    class TextFitEditor {
      +int width
      +int height
      +TextFitEditor(width, height)
    }
    class TextFitEditorState {
      -TextEditingController _controller
      -GlobalKey _repaintKey
      -Color _textColor
      -Color _backgroundColor
      -TextAlign _align
      -List<Color> _availableColors
      +initState()
      +build(context)
      +_calculateCanvas(screenSize)
      +_fitFontSize(text, maxW, maxH, align)
      +_export(canvasSize)
    }
    TextFitEditor o-- TextFitEditorState
Loading

File-Level Changes

Change Details Files
Integrate TextFitEditor into the BottomActionMenu
  • Import TextFitEditor at the top of image_editor.dart
  • Add a new 'Text' action button that navigates to TextFitEditor
  • Receive and handle returned PNG bytes from TextFitEditor
  • Update and finalize the image via imgLoader and trigger onSourceChanged
lib/view/image_editor.dart
Implement TextFitEditor UI and core text-resizing logic
  • Calculate a canvas size that preserves the given aspect ratio
  • Implement a binary-search font-size fitting algorithm to ensure text always fits
  • Build controls for text input, color selection, alignment, and background
  • Capture the widget via RepaintBoundary and export it as PNG bytes
lib/view/text_fit_editor.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#284 Implement a dynamic Text Editor where text always fits inside a fixed canvas by auto-resizing as content grows.
#284 Enable exporting the finalized text as an image, similar to other elements.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Dispose the TextEditingController in TextFitEditorState.dispose to avoid memory leaks.
  • Wrap the editor UI in a SafeArea or handle keyboard insets so the text field and controls aren’t obscured by system UI or the keyboard.
  • Consider extracting the font fitting binary‐search logic (_fitFontSize) into a separate utility to improve reusability and testability.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Dispose the TextEditingController in TextFitEditorState.dispose to avoid memory leaks.
- Wrap the editor UI in a SafeArea or handle keyboard insets so the text field and controls aren’t obscured by system UI or the keyboard.
- Consider extracting the font fitting binary‐search logic (_fitFontSize) into a separate utility to improve reusability and testability.

## Individual Comments

### Comment 1
<location> `lib/view/text_fit_editor.dart:79-83` </location>
<code_context>
+  }
+
+  Future<Uint8List?> _export(Size canvasSize) async {
+    final boundary = _repaintKey.currentContext?.findRenderObject()
+        as RenderRepaintBoundary?;
+    if (boundary == null) return null;
+    final pixelRatio = widget.width / canvasSize.width;
+    final ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
+    final ui.ByteData? data =
+        await image.toByteData(format: ui.ImageByteFormat.png);
</code_context>

<issue_to_address>
**issue (bug_risk):** Check for zero or negative pixelRatio before calling toImage.

A guard clause to ensure pixelRatio is positive will prevent runtime errors if canvasSize.width is zero or negative.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Dynamic Auto-Resizing Text Editor

1 participant