๐งฉ middle_ellipsis_text
A lightweight Flutter widget that truncates long text in the middle with ellipsis (...),
perfect for displaying long filenames, URLs, or email addresses.
โจ Features
โ
Truncate text in the middle, not just at the end
โ
Cache optimization for high performance
โ
Adjustable start/suffix ratio via keepStartFraction
โ
Compatible with all fonts and TextStyle
โ
Fully tested and null-safe
๐ Usage
import 'package:middle_ellipsis_text/middle_ellipsis_text.dart';
SizedBox(
width: 200,
child: const MiddleEllipsisText(
'this_is_a_very_long_filename_that_should_be_cut_in_the_middle.txt',
style: TextStyle(fontSize: 16),
keepStartFraction: 0.6,
),
);
Result:
this_is_a_very_l...in_the_middle.txt
โ๏ธ Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
String |
required | The text to display |
style |
TextStyle? |
DefaultTextStyle |
Text style |
keepStartFraction |
double |
0.5 |
Ratio of visible characters at the start |
๐งช Example App
Run the demo app:
cd example
flutter run
Example:
MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Middle Ellipsis Example')),
body: const Center(
child: SizedBox(
width: 180,
child: MiddleEllipsisText(
'https://example.com/some/really/long/path/to/resource/file.pdf',
style: TextStyle(fontSize: 14),
),
),
),
),
);
๐ง How It Works
MiddleEllipsisText uses TextPainter to measure text width
and then performs a binary search to find the longest prefix+suffix
that fits within the layout constraints.
It intelligently caches:
- The last measured text
- Text style hash
- Width constraints
to avoid redundant computations when rebuilding widgets.
๐งฉ Example Screenshot
| Default (0.5) | Custom (0.8) |
|---|---|
![]() |
![]() |
โ Tests
Run all tests:
flutter test
All cases covered:
- Normal short text (no truncation)
- Long text with ellipsis
- Cache validation
keepStartFractionratio- Very narrow width edge case
๐ License
MIT License ยฉ 2025 dab246
๐ Support
If this widget helps your Flutter project,
please โญ it on GitHub!

