溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Flutter質(zhì)感設(shè)計之表單輸入

發(fā)布時間:2020-08-30 14:31:14 來源:腳本之家 閱讀:170 作者:何小有 欄目:移動開發(fā)

FormField控件是單一表單字段,這個控件維護表單字段的當(dāng)前狀態(tài),以便更新和驗證錯誤能在UI中可見。TextField控件就是在FormField中包裝了一個Input控件(后面的文章講解),F(xiàn)ormField維護輸入的當(dāng)前值,使您不需要自己管理它,更容易一次保存,重置或驗證多個字段。

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:包含輸入的表單控件,每個表單字段都應(yīng)該在FormField控件中
      new TextField(
       labelText: '姓氏',
       // onSaved:當(dāng)通過Form.save()保存表單時調(diào)用的方法
       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重置為初始狀態(tài)
          _formKey.currentState.reset();
          _showMessage('姓名信息已經(jīng)重置');
         }
        ),
        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質(zhì)感設(shè)計之表單輸入

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI