温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Flutter质感设计之表单输入

发布时间:2020-08-30 14:31:14 来源:脚本之家 阅读:161 作者:何小有 栏目:移动开发

FormField控件是单一表单字段,这个控件维护表单字段的当前状态,以便更新和验证错误能在UI中可见。TextField控件就是在FormField中包装了一个Input控件(后面的文章讲解),FormField维护输入的当前值,使您不需要自己管理它,更容易一次保存,重置或验证多个字段。

import 'package:flutter/material.dart';

class MyApp extends StatefulWidget {
 @override
 _MyApp createState() => new _MyApp();
}

class _MyApp extends State<MyApp> {

 String _lastName;
 String _firstName;
 GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

 void _showMessage(String name) {
  showDialog<Null>(
   context: context,
   child: new AlertDialog(
    content: new Text(name),
    actions: <Widget>[
     new FlatButton(
      onPressed: () {
       Navigator.pop(context);
      },
      child: new Text('确定')
     )
    ]
   )
  );
 }

 @override
 Widget build(BuildContext context) {
  return new Scaffold(
   appBar: new AppBar(
    title: new Text('表单输入')
   ),
   // Form:用于将多个表单控件组合在一起的容器
   body: new Form(
    key: _formKey,
    child: new Column(
     children: <Widget> [
      // TextFieldd:包含输入的表单控件,每个表单字段都应该在FormField控件中
      new TextField(
       labelText: '姓氏',
       // onSaved:当通过Form.save()保存表单时调用的方法
       onSaved: (InputValue value) {
        _lastName = value.text;
       }
      ),
      new TextField(
       labelText: '名字',
       onSaved: (InputValue value) {
        _firstName = value.text;
       }
      ),
      new Row(
       children: <Widget> [
        new RaisedButton(
         child: new Text('重置'),
         onPressed: () {
          // reset():将此Form下的每个TextField重置为初始状态
          _formKey.currentState.reset();
          _showMessage('姓名信息已经重置');
         }
        ),
        new RaisedButton(
         child: new Text('提交'),
         onPressed: () {
          // save():保存Form下的每个TextField
          _formKey.currentState.save();
          _showMessage('你的姓名是'+_lastName+_firstName);
         }
        )
       ]
      )
     ]
    )
   )
  );
 }
}

void main() {
 runApp(new MaterialApp(
  title: 'Flutter Demo',
  home: new MyApp()
 ));
}

Flutter质感设计之表单输入

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI