1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to implement numbering list feature in textfield like whatsApp with dart code in...

Discussão em 'Mobile' iniciado por Stack, Outubro 16, 2024 às 05:53.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement numbering list feature to my app like whatsApp does. It almost got implemented but I found 3 bugs in my code.

    Note : I'm not using any outside packages for it, created custom TextEditingController to achieve this.


    1. BUG1


    2. BUG2


    3. BUG3

    Code:

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

    @override
    State<TestingPage> createState() => _TestingPageState();
    }

    class _TestingPageState extends State<TestingPage> {
    CustomTextEditingController controller = CustomTextEditingController();
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: const Text("Testing"),
    centerTitle: true,
    ),
    body: Center(
    child: Padding(
    padding: const EdgeInsets.symmetric(horizontal: 30),
    child: TextField(
    controller: controller,
    maxLines: 8,
    decoration: const InputDecoration(
    hintText: "Enter text here...", border: OutlineInputBorder()),
    ),
    ),
    ),
    );
    }
    }




    Custom TextEditingController class :

    import 'package:flutter/cupertino.dart';

    class CustomTextEditingController extends TextEditingController {
    bool isBackPressed = false;
    String _previousText = '';

    CustomTextEditingController({String text = ''}) : super(text: text) {
    addListener(_onTextChanged);
    }

    // Custom function to handle text changes
    void _onTextChanged() {
    String text = this.text;
    int cursorPosition = selection.baseOffset;

    if (_previousText.length > text.length) {
    isBackPressed = true;
    } else {
    isBackPressed = false;
    }
    _previousText = text;

    if (cursorPosition > 0) {
    // Detect Enter key press
    if (text[cursorPosition - 1] == '\n' && !isBackPressed) {
    debugPrint("inside12");
    String previousLine = _getPreviousLine(text, cursorPosition);
    RegExp numberRegex = RegExp(r'^(\d+)\.\s');

    Match? match = numberRegex.firstMatch(previousLine);
    if (match != null) {
    int currentNumber = int.parse(match.group(1)!);
    String newText = text.replaceRange(
    cursorPosition,
    cursorPosition,
    '${currentNumber + 1}. ',
    );

    value = TextEditingValue(
    text: newText,
    selection: TextSelection.collapsed(
    offset: cursorPosition + currentNumber.toString().length + 2,
    ),
    );
    }
    }
    // Handle backspace to clear line numbering
    if (text[cursorPosition - 1] == '\n' &&
    cursorPosition > 1 &&
    text.substring(cursorPosition - 4, cursorPosition - 1) == '. ') {
    debugPrint("inside11");
    String newText = text.substring(0, cursorPosition - 4) +
    text.substring(cursorPosition);
    value = TextEditingValue(
    text: newText,
    selection: TextSelection.collapsed(offset: cursorPosition - 4),
    );
    }
    }
    }

    @override
    void dispose() {
    // Clean up the listener when the controller is disposed
    removeListener(_onTextChanged);
    super.dispose();
    }

    String _getPreviousLine(String text, int cursorPosition) {
    int lastNewline = text.lastIndexOf('\n', cursorPosition - 2);
    if (lastNewline == -1) {
    return text.substring(0, cursorPosition).trim();
    } else {
    return text.substring(lastNewline + 1, cursorPosition - 1).trim();
    }
    }
    }

    Continue reading...

Compartilhe esta Página