Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: input text field don't work properly in a simple example..... where am I wrong?

Tags:

flutter

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'
          )
      )
    );
  }
}
like image 934
Matteo G. Avatar asked Feb 06 '23 19:02

Matteo G.


1 Answers

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;
              });
            })));
  }
}
like image 118
just95 Avatar answered Feb 09 '23 08:02

just95