flutter_html_search_highlight

πŸ”₯ Highlight and navigate search results inside HTML content in Flutter β€” with built-in UI and zero setup.

Search across HTML pages, highlight matches with <mark>, and give users smooth navigation (next/previous) without writing custom parsing logic.


🎬 Demo

Instantly search, highlight, and navigate across HTML content:

Single Page Demo

Multi Page Demo

✨ Features

  • πŸ” Search plain text inside HTML content
  • 🎯 Highlight matches using <mark> tags
  • πŸ“„ Supports multi-page HTML content
  • ⏭️ Navigate between matches (next / previous)
  • πŸ“Š Match count per page
  • 🧭 Scroll positioning for matches
  • 🎨 Fully customizable UI (search bar, styles, icons)
  • ⚑ Built on flutter_html + HTML parser

πŸš€ Quick Start

import 'package:flutter_html_search_highlight/flutter_html_search_highlight.dart';

HtmlPagedSearchReader(
  htmlContent: "<p>Hello Flutter world</p>",
)

That’s it. No extra setup needed.


πŸ“¦ Installation

dependencies:
  flutter_html_search_highlight: ^0.1.0

πŸ’‘ Why use this?

Instead of building everything manually on top of flutter_html, this package gives you:

βœ… Built-in search UI βœ… Automatic highlighting βœ… Match navigation (next / previous) βœ… Multi-page support βœ… Scroll-to-match logic

All without writing complex parsing or state management.


🧠 Core API (Custom UI)

Use this if you want full control over UI.

final cleaned = pages.map(HtmlSearchHighlighter.cleanHtml).toList();

final result = HtmlSearchHighlighter.searchPages(cleaned, "week's");

// Access results
result.displayPages;
result.matches;
result.matchesPerPage;

Scroll alignment helper

final offset = HtmlSearchHighlighter.scrollOffsetForMatch(
  sourcePageHtml: cleaned[page],
  keyword: keyword,
  matchIndex: 0,
  maxScrollExtent: controller.position.maxScrollExtent,
);

πŸ“– Built-in Paged Reader

Multi-page HTML

HtmlPagedSearchReader(
  htmlPages: const [
    '<p>First page mentions <b>Flutter</b>.</p>',
    '<p>Second page also talks about Flutter.</p>',
  ],
)

Single HTML content

HtmlPagedSearchReader(
  htmlContent: '<h1>One page</h1><p>Search me.</p>',
)

πŸ”„ Content Modes

Force behavior explicitly:

HtmlPagedSearchReader(
  htmlPages: manySections,
  contentMode: HtmlContentMode.singlePage,
  singlePageSeparator: '<hr/>',
)
HtmlPagedSearchReader(
  htmlContent: myHtml,
  contentMode: HtmlContentMode.multiPage,
)

🎨 Customization

Highlight & styles

HtmlPagedSearchReader(
  htmlPages: myPages,
  highlightColor: Colors.orangeAccent,
  markStyle: Style(backgroundColor: Colors.lime),
  htmlStyles: {
    'h1': Style(fontSize: FontSize(22)),
  },
)

Customize search UI

HtmlPagedSearchReader(
  htmlPages: myPages,
  searchFieldBuilder: (context, controller, onChanged, keyword) {
    return TextField(
      controller: controller,
      onChanged: onChanged,
      decoration: const InputDecoration(hintText: 'Search...'),
    );
  },
)

Customize navigation UI

HtmlPagedSearchReader(
  htmlPages: myPages,
  matchNavigationBuilder: (
    context,
    currentMatch,
    totalMatches,
    onPrevious,
    onNext,
  ) {
    return Row(
      children: [
        Text('$currentMatch/$totalMatches'),
        IconButton(onPressed: onPrevious, icon: Icon(Icons.expand_less)),
        IconButton(onPressed: onNext, icon: Icon(Icons.expand_more)),
      ],
    );
  },
)

πŸ“ Example

Check the /example folder for a complete working demo.


βš™οΈ Tips

  • Call cleanHtml() once for better performance
  • Set cleanPagesOnLoad: false if already cleaned
  • Use pagination for large HTML content

🧾 Changelog

0.1.0

✨ Initial release

  • HTML search & highlight
  • Paged reader with navigation
  • Match tracking and scroll support

🀝 Contributing

Contributions, issues, and feature requests are welcome!


πŸ“„ License

This package is licensed under the MIT License. See the LICENSE file for details.


⭐ Support

If this package helps you, consider giving it a ⭐ on GitHub and a πŸ‘ on pub.dev.

Libraries

flutter_html_search_highlight
Search and highlight plain text inside HTML strings, with an optional paged reader built on flutter_html.