import 'package:flutter/material.dart'; import 'package:zhongfa_apps/widget/public/PublicWidget.dart'; class FormTestRoute extends StatefulWidget { @override _FormTestRouteState createState() => new _FormTestRouteState(); } class _FormTestRouteState extends State<FormTestRoute> { TextEditingController selectionController = TextEditingController(); GlobalKey _formKey = new GlobalKey<FormState>(); @override void initState() { super.initState(); selectionController.text="初始值"; } @override Widget build(BuildContext context) { return publicAnimatedTheme( subWidget: Scaffold( appBar: AppBar( title: Text("通過普通的點擊事件修改TextFormField的值"), ), body: Padding( padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0), child: Form( key: _formKey, //設置globalKey,用於后面獲取FormState autovalidate: true, //開啟自動校驗 child: Column( children: <Widget>[ TextFormField( controller: selectionController, decoration: InputDecoration( labelText: "用戶名", hintText: "用戶名或郵箱", icon: Icon(Icons.person), ), // 校驗用戶名 ), // 登錄按鈕 InkWell( onTap: () { selectionController.text="點擊賦值"; }, child: Text("自定義按鈕", style: TextStyle(fontSize: 40)), ) ], ), ), ), )); } }