๐Ÿงฉ 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!