我在做一个登录应用
在登录屏幕上,有三个文本字段:用户号、密码和公司代码(=数据库名称)登录时,输入用户ID、密码和公司代码(=数据库名称),然后单击登录按钮我要通过访问数据库名称中输入的信息的DB来检查ID/密码。
import 'package:flutter/material.dart';
import 'package:hdislogin/screen/menu.dart';
import 'package:postgres/postgres.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final TextEditingController DBnameController = TextEditingController();
final TextEditingController _empl_noController = TextEditingController();
final TextEditingController _passwdController = TextEditingController();
Future<void> _login() async {
//final String DBname = DBnameController.text;
final String empl_no = _empl_noController.text;
final String passwd = _passwdController.text;
PostgreSQLConnection connection = PostgreSQLConnection(
'ip',
5432,
'DBName',
username: 'username',
password: 'password',
);
try {
await connection.open();
final List<List<dynamic>> results = await connection.query(
'SELECT * from table',
substitutionValues: {
'empl_no': empl_no,
'passwd': passwd,
},
);
if (results.isNotEmpty) {
print('success');
Navigator.push(
context,
MaterialPageRoute(builder: (_) => Menu()),
);
} else {
print('fail');
}
} finally {
await connection.close();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(35), //
child: Center(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10),
child: Image.asset(
'assets/images/hdis.png',
height: 300,
width: 300,
),
),
TextField(
maxLength: 4,
//controller: DBnameController = 'DBname',
decoration: InputDecoration(
labelText: 'CompanyCode',
),
),
TextField(
maxLength: 6,
decoration: InputDecoration(
labelText: 'userID',
),
),
TextField(
obscureText: true, //password ***
decoration: InputDecoration(
labelText: 'password',
),
),
Container(
margin: EdgeInsets.only(top: 0),
child: ElevatedButton(
style: OutlinedButton.styleFrom(
minimumSize: Size(70, 30),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => Menu()),
);
},
child: Text(
"Login",
style: TextStyle(
color: Colors.black,
),
),
),
),
], //children
),
),
),
),
);
}
}
目前,代码是ID密码可检查,只有数据库名称不能检查。
如何在TextField中添加接收和检查数据库名称的功能?
我想做的是…
class _LoginPageState extends State<LoginPage>
//added part
final TextEditingController DBnameController = TextEditingController();
.
.
.
TextField(
maxLength: 4,
//added part //controller: DBnameController = 'DBname',
decoration: InputDecoration(
labelText: 'CompanyCode',
),
),
2条答案
按热度按时间watbbzwu1#
你能更新你提供的例子吗?在我看来,您已经注解掉了DBName textfield控制器,并将该字段直接添加到
PostgreSQLConnection
中尝试将其更改为:
}
yh2wf1be2#
执行以下更改