skip to Main Content

I have some form where i can fill in some details like title and description and can save that.

I also can scan some qr code and it search the item if it is already known. What i want is if the qr is not recognized, there is an option that can add the item and prefill the titlefield with the scanned QR(name)..

I have the form here:

void _showFormadd(int? id) async {
    if (id != null) {
      // id == null -> create new item
      // id != null -> update an existing item
      final existingJournal =
      _journals.firstWhere((element) => element['id'] == id);
      _titleController.text = existingJournal['title'];
      _descriptionController.text = existingJournal['description'];
      _aantalController.text = existingJournal['aantal'];
    }



    showModalBottomSheet(
        context: context,
        elevation: 5,
        isScrollControlled: true,
        builder: (_) => Container(
          padding: EdgeInsets.only(
            top: 15,
            left: 15,
            right: 15,
            // this will prevent the soft keyboard from covering the text fields
            bottom: MediaQuery.of(context).viewInsets.bottom + 120,
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              TextField(
                controller: _titleController,


              ),
              const SizedBox(
                height: 10,
              ),
              TextField(
                controller: _descriptionController,
                decoration: const InputDecoration(hintText: 'Description'),
              ),
              const SizedBox(
                height: 20,
              ),
              TextField(
                controller: _aantalController,
                decoration: const InputDecoration(hintText: 'Aantal'),
              ),
              const SizedBox(
                height: 10,
              ),

              ElevatedButton(
                onPressed: () async {
                  // Save new journal
                  if (id == null) {
                    await _addItem();
                  }

                  if (id != null) {
                    await _updateItem(id);
                  }

                  // Clear the text fields
                  _titleController.text = '';
                  _descriptionController.text = '';
                  _aantalController.text = '';

                  // Close the bottom sheet
                  Navigator.of(context).pop();
                },
                child: Text(id == null ? 'Create New' : 'Update'),
              )
            ],
          ),
        ));
  }

When i scan and the item is not found then the scanned code is visible in a field:

Text('Scan result : $_scanBarcoden',
                  textAlign: TextAlign.center,
                  style: const TextStyle(fontSize: 20, height: 30)

My question is, is there some way i can change something in the Textfield _titleController so that the result also can be there already just like in the Text above?

TextField(
   controller: _titleController,

2

Answers


  1. Chosen as BEST ANSWER

    In Setstate i tried something and that is working, the field I want to fill is filled when pushing a button. Only then the tekst filled in is _scanBarcode because it is Text. Is there something I can change so the _scanBarcode scanned result is filled in?:

    setState(() {
          _scanBarcode = barcodeScanRes;
          _runFilter(_scanBarcode);
          _titleController.value = const TextEditingValue(text: "_scanBarcode");
        });
      }
    

  2. Nevermind.. i removed const before TextEditingvalue and it works fine

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search