skip to Main Content
@override
  Widget build(BuildContext context) {
    final AppStateManager manager = AppStateManager.of(context);

    String textLetter = manager.appState.replacementsController.text;
    String textCodeElec = manager.appState.replacementsController.value.text;
    log('data: ${manager.appState.textEditingDeltaHistory.length}');

    if (textLetter == 'a' || textLetter == 'A') {
      textCodeElec = '90'; //a print 90
    }
    if (textLetter == 'b' || textLetter == 'B') {
      textCodeElec = '88';// b print 88
    }
    if (textLetter == 'ao' || textLetter == 'Ao') {
      textCodeElec = '94';// but bao print bao are not 88 94
    }
    // log('textLetter: ${textLetter}');
    log('textCodeElec: ${textCodeElec}');
    return Column(
      children: [
        _buildTextEditingDeltaViewCode(textCodeElec),
        _buildTextEditingDeltaViewHeader(),
        Expanded(
          child: ListView.separated(
            padding: const EdgeInsets.symmetric(horizontal: 35.0),
            itemBuilder: (context, index) {
              return _buildTextEditingDeltaHistoryViews(
                  manager.appState.textEditingDeltaHistory)[index];
            },
            itemCount: manager.appState.textEditingDeltaHistory.length,
            separatorBuilder: (context, index) {
              return const SizedBox(height: 2.0);
            },
          ),
        ),
        const SizedBox(height: 10),
      ],
    );
  }

I have tried many ways but it still does not display as requested when b = 88, ao = 94 but ‘bao = bao’ is not ‘bao = 88 94’.

What can I try next?

2

Answers


  1. bao is a different value than b and ao.
    If you want to display 88 94 when value is bao then you should add:

    if (textLetter == 'bao' || textLetter == 'Bao') {
      textCodeElec = '88 94';
    }
    

    to your code.

    Login or Signup to reply.
  2. Your goal

    When the user types text, it should test if it starts with some key characters (a, b, ao), if this is the case it should add to textCodeElec a number, check if the rest of the value is equal to others key character and add them if it is the case.

    Ecamples:

    • for b it should return 88
    • for bao it should return 88 94
    • for aao it should return 90 94

    The reason why it doesn’t work

    In your conditions, you are using "==" comparator so when you type "bao" it is not equal to "b" or "ao" which is why it returns the same value.

    Solution

    To solve that you can use startsWith which will check if the value start with a key character, if it is the case you will the value of that key and then retrieve that key for the text so you can test it with next key values.

    if (textLetter.toLowerCase().startsWith('a')) {
          textCodeElec += ' 90'; //a print 90
          // remove a from textLetter
          textLetter = textLetter.substring(1);
    }
    if (textLetter.toLowerCase().startsWith('b')) {
          textCodeElec += ' 88';// b print 88
          // remove b from textLetter
          textLetter = textLetter.substring(1);
    }
    if (textLetter.toLowerCase().startsWith('ao')) {
          textCodeElec += ' 94';// but bao print bao are not 88 94
          // remove ao from textLetter
          textLetter = textLetter.substring(2);
    }
    
    //remove all spaces at the beginning and at the end
    textCodeElec = textCodeElec.trim();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search