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)), ) ], ), ), ), )); } }