dart - 键盘显示时 Flutter Form 消失

标签 dart flutter

我用 Flutter 创建了一个 Form,但是当我专注于一个字段并且弹出键盘时,内容消失了。

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:greencat/greencat.dart';
import 'package:woody_app/Application.dart';
import 'package:woody_app/actions/AuthAction.dart';
import 'package:woody_app/states/AuthState.dart';

class ProfileScreen extends StatefulWidget {
  @override
  State createState() => new ProfileScreenState();
}


class PersonData {
  String name = '';
  String phoneNumber = '';
  String password = '';
}

class ProfileScreenState extends State<ProfileScreen> {
  final Store<AuthState, AuthAction<dynamic>> _store = Application.get().store;
  StreamSubscription<AuthState> _subscriber;

  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  PersonData person = new PersonData();

  void showInSnackBar(String value) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(
        content: new Text(value)
    ));
  }

  bool _autovalidate = false;
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  void _handleSubmitted() {
    final FormState form = _formKey.currentState;
    if (!form.validate()) {
      _autovalidate = true;  // Start validating on every change.
      showInSnackBar('Please fix the errors in red before submitting.');
    } else {
      form.save();
      showInSnackBar('${person.name}\'s phone number is ${person.phoneNumber}');
    }
  }

  String _validateName(String value) {
    if (value.isEmpty)
      return 'Name is required.';
    final RegExp nameExp = new RegExp(r'^[A-za-z ]+$');
    if (!nameExp.hasMatch(value))
      return 'Please enter only alphabetical characters.';
    return null;
  }

  @override
  void initState() {
    super.initState();

    _subscriber = _store.stream.listen((AuthState state) {
      setState(() {});
    });
  }


  @override
  void dispose() {
    _subscriber.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Container(child: new Form(
        key: _formKey,
        autovalidate: _autovalidate,
        child: new ListView(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          children: <Widget>[
            new TextFormField(
              decoration: const InputDecoration(
                icon: const Icon(Icons.person),
                hintText: 'What do people call you?',
                labelText: 'Name *',
              ),
              onSaved: (String value) {
                person.name = value;
              },
              validator: _validateName,
            ),
            new Container(
              padding: const EdgeInsets.all(20.0),
              alignment: const FractionalOffset(0.5, 0.5),
              child: new FlatButton(
                child: const Text('SUBMIT'),
                onPressed: _handleSubmitted,
              ),
            ),
            new Container(
              padding: const EdgeInsets.only(top: 20.0),
              child: new Text('* indicates required field', style: Theme
                  .of(context)
                  .textTheme
                  .caption),
            ),
          ],
        )
    ));
  }
}

最佳答案

在你的 Scafold() 中试试这个

resizeToAvoidBottomPadding: false,

关于dart - 键盘显示时 Flutter Form 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45194343/

相关文章:

flutter - 如何在下面的橙色矩形中居中放置标签?

dart - 可拖动项目总是下降 80px 左右

Flutter Custom Painter 和键盘问题

dart - 一页上有两个BwuDatagrids

flutter - 如何在 flutter 中使用 web_socket_channel 监听来自 php MySQL 服务器的更改

android - Flutter:Geolocator返回在空调用方法 'compareTo'

algorithm - 如何查找从现在开始到特定工作日的最近的将来日期?

firebase - flutter : How to implement a callback

android - 在 Flutter 上捕获 Android 后退按钮事件

dart - 如何组织我的 Dart 项目