π§© 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!

