Ad

Flutter: Input Text Field Don't Work Properly In A Simple Example..... Where Am I Wrong?

- 1 answer

I made a simple example with an input text, but on my phone (nokia edge 6) neither on an older tablet (samsung) don't work.... 2 problem:

1) if I tap on the input, the string inserted (as soon as I write some text) doesn't appear in the input

2) if use an initial value for the input, if I tap in it, I cannot write text and I cannot position the cursor where I want (it goes only before other text inside the input)

how can I do? thanks very much!

Here is my code:

import 'package:flutter/material.dart';

void main() {
  runApp(
    new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue
      ),
      home: new FlutterDemo()
    )
  );
}

class FlutterDemo extends StatefulWidget {
  FlutterDemo({ Key key }) : super(key: key);

  @override
  _FlutterDemoState createState() => new _FlutterDemoState();
}

class _FlutterDemoState extends State<FlutterDemo> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Input Demo')
      ),
      body: new Center(
        child:
          new Input(
            value: new InputValue(text: 'via Dei Giacinti n° 8'),
            labelText: 'INDIRIZZO'
          )
      )
    );
  }
}
Ad

Answer

As far as I understand the problem is that every time the user edits the text (or changes the position of the cursor) the state of the widget changes such that it is rebuilt. When the widget is built you specify the text of the Input, thus it never changes.

The following should work:

class _FlutterDemoState extends State<FlutterDemo> {
  InputValue inputValue = new InputValue(text: 'via Dei Giacinti n° 8');

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(title: new Text('Input Demo')),
        body: new Center(child: new Input(
            value: inputValue,
            labelText: 'INDIRIZZO',
            onChanged: (InputValue newInputValue) {
              setState(() {
                inputValue = newInputValue;
              });
            })));
  }
}
Ad
source: stackoverflow.com
Ad