import 'package:flutter/material.dart';标签:username,const,context,dart,key,5.16,TextEditingController From: https://www.cnblogs.com/kongxiangzeng/p/18246559
import 'package:test1/Do/UserDao.dart'; // Make sure this path is correct
import 'package:test1/page/logined.dart';
import 'register.dart'; // Import your RegisterPage class
class LoginPage extends StatelessWidget {
LoginPage({required Key key}) : super(key: key);
// Move TextEditingController declaration outside of build method
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('登录'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextFormField(
controller: usernameController, // assign controller
decoration: InputDecoration(
labelText: '用户名',
),
),
const SizedBox(height: 20.0),
TextFormField(
controller: passwordController, // assign controller
decoration: const InputDecoration(
labelText: '密码',
),
obscureText: true,
),
const SizedBox(height: 20.0),
ElevatedButton(
onPressed: () {
String username = usernameController.text.trim();
String password = passwordController.text.trim();
// Remove extra condition check
if (username.isNotEmpty && password.isNotEmpty) {
// 调用后端登录函数
login(context, username, password);
// Navigate to MyApp2 with username parameter
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyApp2(username: username),
),
);
} else {
// 提示用户输入完整的信息
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('请输入完整的信息'),
),
);
}
},
child: const Text('登录'),
),
const SizedBox(height: 10.0),
TextButton(
onPressed: () {
// Navigate to the RegisterPage
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RegisterPage(key: UniqueKey())),
);
},
child: const Text('注册'),
),
],
),
),
);
}
}