実装
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> {
TextEditingController _usernameController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
bool _isLoginButtonEnabled = false;
@override
void initState() {
super.initState();
_usernameController.addListener(_checkButtonStatus);
_passwordController.addListener(_checkButtonStatus);
}
@override
void dispose() {
_usernameController.dispose();
_passwordController.dispose();
super.dispose();
}
// ボタンのdisable判定
void _checkButtonStatus() {
setState(() {
_isLoginButtonEnabled = _usernameController.text.isNotEmpty &&
_passwordController.text.isNotEmpty;
});
}
void _login() {
String username = _usernameController.text;
String password = _passwordController.text;
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Login Information'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Username: $username'),
Text('Password: $password'),
],
),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Page'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextField(
controller: _usernameController,
decoration: InputDecoration(
labelText: 'Username',
),
),
SizedBox(height: 16.0),
TextField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
),
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: _isLoginButtonEnabled ? _login : null,
child: Text('Login'),
),
],
),
),
);
}
}
この記事は役に立ちましたか?
貴重なフィードバックありがとうございます!