表单录入
表单录入
如何检索用户输入?
Xamarin.Forms 的元素允许您直接查询元素来确定它的任何属性的状态,或者它被绑定到视图模型中的属性。
在 Flutter 中检索信息是由专门的 widget 处理的,这是跟原来的习惯不同的。如果你有一个 TextField 或 TextFormField ,你可以提供一个 TextEditingController 来检索用户输入:
class _MyFormState extends State<MyForm> {
// Create a text controller and use it to retrieve the current value
// of the TextField.
final myController = new TextEditingController();
@override
void dispose() {
// Clean up the controller when disposing of the widget.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Retrieve Text Input'),
),
body: new Padding(
padding: const EdgeInsets.all(16.0),
child: new TextField(
controller: myController,
),
),
floatingActionButton: new FloatingActionButton(
// When the user presses the button, show an alert dialog with the
// text that the user has typed into our text field.
onPressed: () {
return showDialog(
context: context,
builder: (context) {
return new AlertDialog(
// Retrieve the text that the user has entered using the
// TextEditingController.
content: new Text(myController.text),
);
},
);
},
tooltip: 'Show me the value!',
child: new Icon(Icons.text_fields),
),
);
}
}
你可以在 Flutter 实用教程 中的 获取文本框的输入值 找到更多的信息和完整的代码清单。
在入口的占位符 (Placeholder) 与什么等价?
在 Xamarin.Forms 中,一些元素支持占位符(Placeholder)属性,可以给它赋一个值。如:
<Entry Placeholder="This is a hint">
在 Flutter 中,通过在文本 widget 的装饰器构造函数参数中添加 InputDecoration 对象,可以轻松地为输入显示“提示”或占位符文本。
body: new Center(
child: new TextField(
decoration: new InputDecoration(hintText: "This is a hint"),
)
)
如何显示验证错误?
使用 Xamarin.Forms 时,如果您希望提供验证错误的可视化提示,则需要创建新属性和 虚拟元素(VisualElement) 来包围具有验证错误的元素。
在 Flutter 中,我们将 InputDecoration 对象传递给文本 widget 的装饰器构造函数。
然而,您不希望从显示错误开始。相反,当用户输入无效数据时,应该更新状态,并传递一个新的 InputDecoration 对象。
import 'package:flutter/material.dart';
void main() {
runApp(new SampleApp());
}
class SampleApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Sample App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new SampleAppPage(),
);
}
}
class SampleAppPage extends StatefulWidget {
SampleAppPage({Key key}) : super(key: key);
@override
_SampleAppPageState createState() => new _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage> {
String _errorText;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Sample App"),
),
body: new Center(
child: new TextField(
onSubmitted: (String text) {
setState(() {
if (!isEmail(text)) {
_errorText = 'Error: This is not an email';
} else {
_errorText = null;
}
});
},
decoration: new InputDecoration(hintText: "This is a hint", errorText: _getErrorText()),
),
),
);
}
_getErrorText() {
return _errorText;
}
bool isEmail(String em) {
String emailRegexp =
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regExp = new RegExp(emailRegexp);
return regExp.hasMatch(em);
}
}
