🧩 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!