hyphen 0.2.0 copy "hyphen: ^0.2.0" to clipboard
hyphen: ^0.2.0 copied to clipboard

A cross-platform Flutter plugin that provides high-quality word hyphenation.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:hyphen/hyphen.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  /// Here we store both hyphen instances (for UTF-8 and ISO8859-encoded dictionaries) in order
  /// to be able to dispose them later.
  ///
  /// Usually you'll only need one of those, this is just for the example.
  List<Hyphen> hyphens = [];

  final List<String> words = [
    "funktioniert",
    "Arbeit",
    "ankleiden",
    "abarbeiten",
    "ableiten",
    "anders",
    "arbeiten",
    "hineinschauen",
    "vorankommen",
    "schwören",
    "schworen",
  ];

  _HyphenationResults? hyphenationResultsIso8859;
  _HyphenationResults? hyphenationResultsUtf8;

  Future<_HyphenationResults> _hyphenateWithDictionary(
    String dictionaryPath,
  ) async {
    Hyphen hyphen = await Hyphen.fromDictionaryPath(dictionaryPath);
    hyphens.add(hyphen);

    List<String> hyphenateResults = [];
    List<String> hyphenateResultsLegacy2 = [];
    List<String> hyphenateResultsLegacy3 = [];

    for (String word in words) {
      /// Main hyphenation function which combines hnj_hyphenate2 and hnj_hyphenate3
      final result = hyphen.hyphenate(word, lhmin: 3, rhmin: 3);
      hyphenateResults.add(result.join("-"));

      /// Legacy API 2, using hnj_hyphenate2
      final resultLegacy2 = hyphen.hnjHyphenate2(word);
      hyphenateResultsLegacy2.add(resultLegacy2);

      /// Legacy API 3, using hnj_hyphenate3
      final resultLegacy3 = hyphen.hnjHyphenate3(word, lhmin: 3, rhmin: 3);
      hyphenateResultsLegacy3.add(resultLegacy3);
    }

    return _HyphenationResults(
      results: hyphenateResults,
      resultsLegacyApi2: hyphenateResultsLegacy2,
      resultsLegacyApi3: hyphenateResultsLegacy3,
    );
  }

  @override
  void initState() {
    super.initState();

    Future.wait([
      _hyphenateWithDictionary(
        "assets/hyph_de_DE_UTF.dic",
      ), // UTF8-encoded dictionary
      _hyphenateWithDictionary(
        "assets/hyph_de_DE.dic",
      ), // ISO8859-encoded dictionary
    ]).then((value) {
      setState(() {
        hyphenationResultsUtf8 = value[0];
        hyphenationResultsIso8859 = value[1];
      });
    });
  }

  @override
  void dispose() {
    for (var e in hyphens) {
      e.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: SingleChildScrollView(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                hyphenationResultsUtf8 != null
                    ? _HyphenationResultsWidget(
                      results: hyphenationResultsUtf8!,
                      encoding: "UTF-8",
                    )
                    : CircularProgressIndicator(),
                hyphenationResultsIso8859 != null
                    ? _HyphenationResultsWidget(
                      results: hyphenationResultsIso8859!,
                      encoding: "ISO8859",
                    )
                    : CircularProgressIndicator(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _HyphenationResultsWidget extends StatelessWidget {
  final _HyphenationResults results;
  final String encoding;

  const _HyphenationResultsWidget({
    required this.results,
    required this.encoding,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          encoding,
          style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
        ),
        SizedBox(height: 24),
        _SingleHyphenationResultWidget(
          title: "Hyphenated words:",
          hyphenatedWords: results.results,
        ),
        SizedBox(height: 16),
        _SingleHyphenationResultWidget(
          title: "Hyphenated words legacy API 2:",
          hyphenatedWords: results.resultsLegacyApi2,
        ),
        SizedBox(height: 16),
        _SingleHyphenationResultWidget(
          title: "Hyphenated words legacy API 3:",
          hyphenatedWords: results.resultsLegacyApi3,
        ),
      ],
    );
  }
}

class _SingleHyphenationResultWidget extends StatelessWidget {
  final List<String> hyphenatedWords;
  final String title;

  const _SingleHyphenationResultWidget({
    required this.hyphenatedWords,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        ),
        SizedBox(height: 8),
        ...hyphenatedWords.map((e) => Text(e)),
      ],
    );
  }
}

class _HyphenationResults {
  final List<String> results;
  final List<String> resultsLegacyApi2;
  final List<String> resultsLegacyApi3;

  _HyphenationResults({
    required this.results,
    required this.resultsLegacyApi2,
    required this.resultsLegacyApi3,
  });
}
2
likes
150
points
534
downloads

Publisher

unverified uploader

Weekly Downloads

A cross-platform Flutter plugin that provides high-quality word hyphenation.

Repository (GitHub)
View/report issues

Topics

#hyphenation #hyphen #ffi #wasm

Documentation

API reference

License

unknown (license)

Dependencies

characters, ffi, flutter, http, path, plugin_platform_interface, web

More

Packages that depend on hyphen

Packages that implement hyphen