user9139407
user9139407

Reputation: 1022

How to add initial value without edit initial value on Flutter?

I added initial value like this, But user can edit initial text. How to disable to edit initial value? But user can be able to add value with initial value?

 var _myController = TextEditingController(text: "https://");

Output should be like this

print(url);

https://(user_type_value)

Upvotes: 0

Views: 263

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 268114

enter image description here

You can try this logic, this way https:// will always be shown to the user, if user enters a url without https:// we are good, and if a user enters url with https:// we are again good.

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    TextField(
      controller: _controller,
      decoration: InputDecoration(prefixText: "https://"),
    ),
    RaisedButton(
      child: Text("Submit"),
      onPressed: () {
        String text = _controller.text.toString();
        if (!text.contains("https://")) {
          text = "https://" + text;
        }
        // text here will always have https://
      },
    ),
  ],
),

Upvotes: 2

user1209216
user1209216

Reputation: 7914

As far as I understand, you need something like masked edit, look here: https://github.com/benhurott/flutter-masked-text/blob/master/README.md

Upvotes: 0

Related Questions