A lightweight custom function for FlutterFlow that safely truncates any string to a max character count — and appends '...' automatically if needed.
Before / after
input (original)
"This is a very long title that keeps going forever"
50 chars
output (maxChars: 35)
"This is a very long title that..."
35 chars + "..."
What's included
Integration
In your FlutterFlow project, go to Custom Code → Custom Functions → + Create. Name it exactly `truncateText`.
Add two arguments: `input` (String) and `maxChars` (int). Set the return type to String.
Paste the function code below and hit Save. FlutterFlow will compile it automatically.
Call `truncateText()` in any action, condition, or widget property that accepts a String. Pass your text and the character limit.
Function signature
| Parameter | Type | Default | Description |
|---|---|---|---|
| input | String | — | The text string to truncate |
| maxChars | int | — | Maximum number of characters before truncation |
Ready to copy
truncateText(
input: postTitle,
maxChars: 60,
)truncateText(
input: description,
maxChars: 120,
)truncateText(
input: categoryName,
maxChars: 20,
)truncateText(
input: lastMessage,
maxChars: 45,
)Full source
Copy the entire file into FlutterFlow → Custom Code → Custom Widgets.
// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom function code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
String truncateText(
int maxChars,
String input,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
if (maxChars <= 0) return '';
if (input.isEmpty) return '';
if (input.length <= maxChars) return input;
return '${input.substring(0, maxChars)}...';
/// MODIFY CODE ONLY ABOVE THIS LINE
}From the trenches
Combine with a Text widget:: Use the output directly as the `text` property — no extra logic needed.
Dynamic limits:: Pass `maxChars` from a page state variable to change the limit based on layout.
Null safety:: The function handles empty strings — no need to wrap it in null checks.
Pair with 'Read more':: Show truncated text by default and reveal full text on tap using a boolean state.
Free & open source
Drop-in shimmer loading animation. Five layout presets, fully customizable.
Customizable dotted and dashed borders. Because FlutterFlow doesn't have this built in.
Profile image with initials fallback. Same name always gets the same color.
3D flip animation between front and back content. Tap to reveal.
Building an app?
I build complete apps for founders — fixed prices, fast delivery. Book a free 30-minute call and let's talk about your idea.
Book a free call