首页 > 解决方案 > 在 textFormField 上打开 Flutter 重定向

问题描述

每当我单击 textFormField 时,键盘几乎立即打开和关闭,我认为它有点刷新页面。我有另一个页面,其中包含我没有遇到此问题的表单。这是该页面的代码,我在应用程序中的另一种形式几乎与此相同

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

import '../scoped_models/products.dart';

class ProductAdd extends StatelessWidget {
  final _formData = {
    'title': null,
    'description': null,
    'price': null,
    'image': 'assets/food.jpg'
  };
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  Widget _titleField() {
    return TextFormField(
      decoration: InputDecoration(labelText: 'Enter Title'),
      keyboardType: TextInputType.text,
      validator: (String value) {
        if (value.isEmpty) {
          return 'Please enter a valid title';
        }
      },
      onSaved: (value) {
        print(value);
        _formData['title'] = value;
      },
    );
  }

  Widget _descriptionField() {
    return TextFormField(
      decoration: InputDecoration(labelText: 'Enter Description'),
      keyboardType: TextInputType.text,
      validator: (String value) {
        if (value.isEmpty) {
          return 'Please enter a valid description';
        }
      },
      onSaved: (value) {
        print(value);
        _formData['description'] = value;
      },
    );
  }

  Widget _priceField() {
    return TextFormField(
      decoration: InputDecoration(labelText: 'Enter Price'),
      keyboardType: TextInputType.number,
      validator: (String value) {
        if (value.isEmpty) {
          return 'Please enter a valid price';
        }
      },
      onSaved: (value) {
        print(value);
        _formData['price'] = value;
      },
    );
  }

  Widget _submitButton(context) {
    return RaisedButton(
      textColor: Colors.white,
      child: Text('LOGIN'),
      onPressed: () {
        if (!_formKey.currentState.validate()) {
          return;
        }
        _formKey.currentState.save();
        Navigator.pushReplacementNamed(context, '/products');
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<ProductsModel>(
      builder: (context, child, ProductsModel model) {
        return Container(
          child: Center(
            child: Container(
              child: SingleChildScrollView(
                child: Container(
                  padding: EdgeInsets.all(20.0),
                  child: Form(
                    key: _formKey,
                    child: Column(
                      children: <Widget>[
                        _titleField(),
                        _descriptionField(),
                        _priceField(),
                        SizedBox(
                          height: 10.0,
                        ),
                        _submitButton(context)
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}

我正在使用颤振版本:1.0.0

标签: flutter

解决方案


这是我的更正,希望它有效;你需要在 TextFormField 中添加 TextEditingController titlecontroller,不要使用 onsaved() ;并在提交按钮功能上使用:

  TextEditingController _pricecontroller;
 Widget _priceField() {
    return TextFormField(
//addcontroller;
controller : __pricecontroller
      decoration: InputDecoration(labelText: 'Enter Price'),
      keyboardType: TextInputType.number,
      validator: (String value) {
        if (value.isEmpty) {
          return 'Please enter a valid price';
        }
      },
      onSaved: (value) {
        print(value);
        _formData['price'] = value;
      },
    );
  }

 Widget _submitButton(context) {
    return RaisedButton(
      textColor: Colors.white,
      child: Text('LOGIN'),
      onPressed: () {
        if (!_formKey.currentState.validate()) {
            _formKey.currentState.save();
        _formData['title'] = __titlecontroller.text;
        _formData['description'] = __desccontroller.text;
        _formData['price'] = __pricecontroller.text;
        }
        Navigator.pushReplacementNamed(context, '/products');
      },
    );
  } 

推荐阅读