Home > OS >  How to show text to TextField but not include in onSaved?
How to show text to TextField but not include in onSaved?

Time:10-08

How to show text to TextField but not include in onSaved ?

i want create textFieldCounter have return in onSaved actually string.. i return number in string format.. so, i can parse to onSaved to int if i use this widget

but, this design have string inside textfield like "Orang"

expect :

TextFieldCounter

i've try create TextFieldWithCounter

Actual :

TextFieldCounter

i try add suffixText. but, suffixText position end of TextField :

TextFieldCounter

my code :

class _TextFieldCounterState extends State<TextFieldCounter> {
  int _counter = 1;

  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.text =
        widget.value != null ? widget.value.toString() : _counter.toString();
    _counter = widget.value ?? 1;
  }

  @override
  Widget build(BuildContext context) {
    return OutlineTextField(
      readOnly: true,
      textEditingController: _controller,
      labelText: widget.labelText,
      onSaved: widget.onSaved,
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          HOutlinedButton.icon(
            onPressed: _counter == 1
                ? null
                : () {
                    setState(() {
                      if (_counter > 1) _counter--;
                      _controller.text = _counter.toString();
                    });
                  },
            icon: Icon(
              HelperIcons.mines,
              size: 20.0,
            ),
          ),
          SizedBox(width: 8),
          HOutlinedButton.icon(
            onPressed: () {
              setState(() {
                _counter  ;
                _controller.text = _counter.toString();
              });
            },
            icon: Icon(
              HelperIcons.plus,
              size: 20.0,
            ),
          ),
          SizedBox(width: 12.0),
        ],
      ),
    );
  }
}

note :

i've try add string _controller.text = _counter.toString() ' Orang';but, in onSaved will return 1 Orang. so, i can't parse to int

CodePudding user response:

use

onSaved: () {
     return _counter.toString().split(" ")[0];
}

Now it will return the integer part only as a string and you can parse it to int.

CodePudding user response:

You can use blank space in suffixtext but its not practical

suffixText("Orang"),

before

after using

suffixText("Orang ")

after

or

you can use stack.

Put container and textfield in stack

Stack(children: [Container(
                            //adjust according to your widget
                                  padding:
                                      EdgeInsets.only(left: 20, right: 20),
                                  margin: EdgeInsets.only(
                                      left: 40, right: 15, top: 15),
                                  child: Text(
                                    "Orang",
                                    style: TextStyle(fontSize: 18),
                                  )),
                                  OutlineTextField(
                                    //your textfield
                                  )],),

using stack

CodePudding user response:

You can use onsaved a empty String ""

return _counter.toString().split(" ")[0];
  • Related