2017-11-21 11 views
2

フォームを画面からスクロールすると、各フォームフィールドの状態を保存できますか?スクロールアップしてフィールドが画面外になると、TextFormFieldsに入力した値が失われます。フォームがスクリーンからスクロールされたときに各フォームフィールドの状態が失われますか?

私はコントローラーにそれを保存し、コードに見られるようにTextFormFieldの初期値に割り当てるという提案を聞いたことがありますが、それは私のためには機能しません。私は何が欠けていますか?

TextEditingController _controller = new TextEditingController(); 

@override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     key: scaffoldKey, 
     appBar: new AppBar(
     title: new Text('Enter Vehicle'), 
    ), 
     body: new Padding(
      padding: const EdgeInsets.all(16.0), 
      child: new Form(
       key: formKey, 
       child: new ListView(children: <Widget>[ 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Vehicle Number'), 
        controller: _controller, 
        initialValue: _controller.text, 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Tag'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Make'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Model'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Year'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Vehicle Type'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Location'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Fuel'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Other'), 
       ), 
       new TextFormField(
        decoration: new InputDecoration(labelText: 'Your password'), 
        validator: (val) => 
        val.length < 6 ? 'Password too short.' : null, 
        onSaved: (val) => _password = val, 
        obscureText: true, 
       ), 
       new RaisedButton(
        onPressed: _submit, 
        child: new Text('Login'), 
       ), 
       ],) 
     ) 
    ), 
    ); 
    } 

答えて

0

あなたは、各TextFormField専用のTextEditingControllerを必要と私はあなたがフィールド上initialValue属性を持っている必要はないと思います。

この回答を見てくださいhttps://stackoverflow.com/a/45242235/6414732、それも同様に役立つ可能性があります。

+0

これは巨大な痛みのようです。フォームフィールドを使用するのは簡単ではなく、代わりにカスタムの "反応フォーム"を作成する方が簡単です。 – Darky

+0

リストビューを使用しないで、リストビューを使用しないでください。リストが何百ものフィールドでない限り、これは問題ではありません。 –

+1

実際、私はすでにそれを試みていました。各TextFormFieldにTextEditingControllerがあります。 build()の中で、各フィールドのコントローラーを関連するフィールド値に割り当て、次に各フィールド値を関連するTextFormFieldの初期値に割り当てました。しかし、それでも** setState **が永続的な値として呼び出されない限り、それはまだ機能しません。それは正しいとは思わない。フォームがどのように処理されるかを改善するオープンチケットがあることはわかっています。これは私が思っていたよりも仕事に就くための大きな仕事でした。おそらくより良い解決策がいくつかの時点で発生します。それまでの間、私は自分のハッキーな解決策と一緒にやります。 – baksoy

0
new Container(
        padding: const EdgeInsets.only(
         left: 20.0, right: 20.0, top: 0.0, bottom: 0.0), 
        child: new EnsureVisibleWhenFocused(
        focusNode: _focusNode_nooftickets, 
        child: new TextFormField(
         controller: _ticketscontroller, 
         initialValue: _ticketscontroller.text, 
         focusNode: _focusNode_nooftickets, 
         keyboardType: TextInputType.number, 
         inputFormatters: <TextInputFormatter>[ 
         WhitelistingTextInputFormatter.digitsOnly 
         ], 
         validator: validatenooftickets, 
         onSaved: (String value) { 
         ticketmode.nooftickets = value; 
         }, 
        ), 
       ), 
       ), 
関連する問題