content: import 'package:flutter/material.dart';
  import 'package:toolbox/gen/strings.g.dart';
  
  class TextCounterPage extends StatefulWidget {
    const TextCounterPage({super.key});
  
    @override
    State<TextCounterPage> createState() => _TextCounterPage();
  }
  
  class _TextCounterPage extends State<TextCounterPage> {
  
    final TextEditingController _controller = TextEditingController();
  
    int getTextLength() {
      return _controller.text.runes.length;
    }
  
    int getTrimmedTextLength() {
      return _controller.text.trim().runes.length;
    }
  
    int getRawTextLength() {
      return _controller.text.length;
    }
  
    int getLettersCount() {
      return _controller.text.replaceAll(RegExp(r'[^a-zA-ZÀ-ÿ]'), "").length;
    }
  
    int getWordCount() {
      return _controller.text.split(RegExp(r'\s+')).where((element) => element.contains(RegExp(r'[a-zA-ZÀ-ÿ0-9]'))).length;
    }
  
    int getSpaceCount() {
      return _controller.text.replaceAll(RegExp(r'[^ ]'), "").length;
    }
  
    int getDigitCount() {
      return _controller.text.replaceAll(RegExp(r'[^0-9]'), "").length;
    }
  
    int getLineCount() {
      return _controller.text.split("\n").where((element) => element.isNotEmpty).length;
    }
  
    int getEmptyLineCount() {
      if (_controller.text.isEmpty) {
        return 0;
      }
      return _controller.text.split("\n").where((element) => element.isEmpty).length;
    }
  
    int getTotalLineCount() {
      return getLineCount() + getEmptyLineCount();
    }
  
    @override
    Widget build(BuildContext context) {
      return GestureDetector(
          onTap: () {
            FocusManager.instance.primaryFocus?.unfocus();
          },
          child: Scaffold(
            appBar: AppBar(
              title: Text(t.tools.textcounter.title),
              actions: [
                IconButton(
                  icon: const Icon(Icons.delete_outline),
                  tooltip: t.tools.textcounter.clear,
                  onPressed: () {
                    _controller.clear();
                    setState(() {});
                  },
                ),
              ],
            ),
            body: SafeArea(
                child: SingleChildScrollView(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: SingleChildScrollView(
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Padding(
                              padding: const EdgeInsets.fromLTRB(0, 0, 0, 8),
                              child: TextField(
                                controller: _controller,
                                decoration: InputDecoration(
                                  border: const OutlineInputBorder(),
                                  labelText: t.tools.textcounter.enter_text_here,
                                ),
                                maxLines: 10,
                                minLines: 1,
                                keyboardType: TextInputType.multiline,
                                onChanged: (String value) {
                                  setState(() {});
                                },
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.length,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getTextLength().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.trimmed_length,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getTrimmedTextLength().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.raw_length,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getRawTextLength().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.letters,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getLettersCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.words,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getWordCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.spaces,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getSpaceCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.digits,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getDigitCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.lines,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getLineCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.empty_lines,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getEmptyLineCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                              child: Divider(),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    t.tools.textcounter.total_lines,
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    getTotalLineCount().toString(),
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ]
                      ),
                    ),
                  ),
                )),
          )
      );
    }
  }