import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
late TextEditingController _usernameController;
String username = "zhangsan";
String password = "";
@override
void initState() {
super.initState();
_usernameController = TextEditingController.fromValue(
TextEditingValue(
text: username,
selection: TextSelection.fromPosition(
TextPosition(
affinity: TextAffinity.downstream, offset: username.length),
),
),
);
}
@override
void dispose() {
_usernameController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('TextField'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: ListView(
children: [
TextField(
controller: _usernameController,
decoration: const InputDecoration(
hintText: "用户名",
border: OutlineInputBorder(),
),
onChanged: (value) => setState(() {
username = value;
}),
),
const SizedBox(height: 40),
TextField(
obscureText: true,
decoration: const InputDecoration(
hintText: "密码",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.visibility),
),
onChanged: (value) {
password = value;
},
),
const SizedBox(height: 40),
ElevatedButton(
onPressed: () {
print(username);
print(password);
},
child: const Text("登录"),
),
],
),
),
),
);
}
}