All functions
Function

Truncate Text

A lightweight custom function for FlutterFlow that safely truncates any string to a max character count — and appends '...' automatically if needed.

FlutterDartFlutterFlow
View on GitHub

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

Features

  • ✂️Trims any string to a max character count
  • 💬Appends '...' automatically when truncated
  • 🛡️Safe — handles null/empty strings without crashing
  • Zero dependencies — pure Dart
  • 🔁Works anywhere in FlutterFlow custom logic
  • 📱Perfect for feeds, titles, and card previews

Integration

How to use it

1

Create a Custom Function

In your FlutterFlow project, go to Custom Code → Custom Functions → + Create. Name it exactly `truncateText`.

2

Add the arguments

Add two arguments: `input` (String) and `maxChars` (int). Set the return type to String.

3

Paste the code & save

Paste the function code below and hit Save. FlutterFlow will compile it automatically.

4

Use it anywhere

Call `truncateText()` in any action, condition, or widget property that accepts a String. Pass your text and the character limit.

Function signature

Arguments

ParameterTypeDefaultDescription
inputStringThe text string to truncate
maxCharsintMaximum number of characters before truncation

Ready to copy

Preset examples

Feed / Post Title

dart
truncateText(
  input: postTitle,
  maxChars: 60,
)

Card Preview Text

dart
truncateText(
  input: description,
  maxChars: 120,
)

Short Label / Tag

dart
truncateText(
  input: categoryName,
  maxChars: 20,
)

Chat Message Preview

dart
truncateText(
  input: lastMessage,
  maxChars: 45,
)

Full source

Full widget code

Copy the entire file into FlutterFlow → Custom Code → Custom Widgets.

dart
// 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

Pro tips

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.

Building an app?

Need a full MVP, not just widgets?

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