[译] Flutter 从 0 到 1, 第二部分
在这个Flutter系列教程中,我们将从零开始构建一个完整的应用程序。这是第二部分,我们将介绍如何使用Flutter构建一个登录页面。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Card(
margin: EdgeInsets.all(20),
child: SingleChildScrollView(
padding: EdgeInsets.all(16),
child: Column(
children: <Widget>[
Text(
'Login',
style: TextStyle(fontSize: 28),
),
SizedBox(height: 20),
Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
controller: _usernameController,
decoration: InputDecoration(labelText: 'Username'),
validator: (value) {
if (value.isEmpty) {
return 'Username is required';
}
},
),
SizedBox(height: 10),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'Password is required';
评论已关闭