middle_ellipsis_text 1.0.0 copy "middle_ellipsis_text: ^1.0.0" to clipboard
middle_ellipsis_text: ^1.0.0 copied to clipboard

A lightweight Flutter widget that truncates long text with ellipsis (...) in the middle — perfect for filenames, URLs, and long identifiers.

🧩 middle_ellipsis_text #

Pub Version Flutter Test License: MIT Coverage Status

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)
default custom

✅ Tests #

Run all tests:

flutter test

All cases covered:

  • Normal short text (no truncation)
  • Long text with ellipsis
  • Cache validation
  • keepStartFraction ratio
  • Very narrow width edge case

📄 License #

MIT License © 2025 dab246


🌟 Support #

If this widget helps your Flutter project,
please ⭐ it on GitHub!

1
likes
150
points
550
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight Flutter widget that truncates long text with ellipsis (...) in the middle — perfect for filenames, URLs, and long identifiers.

Repository (GitHub)
View/report issues

Topics

#text #widget #ellipsis #flutter-ui

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on middle_ellipsis_text