Arkdaşlar birden fala texfieldform var .bunları class kullanarak kısatmak istiyorum ama bir türlü yapamadım yardımcı olursanız çok iyi olur .
import 'package: flutter/material.dart';
import 'package:widget_examples/authentication_pages/signin_page.dart';a
import 'package:widget_examples/app_pages/Home_Pag.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
// use this controller to get what the user typed
final _textController = TextEditingController();
//final _passwordController = TextEditingController();
bool _showPassword = false;
void _togglevisibilitiy() {
setState(() {
_showPassword = !_showPassword;
});
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.green,
Color.fromARGB(255, 15, 104, 177),
Color.fromARGB(255, 151, 53, 46),
Color.fromARGB(255, 114, 17, 131)
])),
child: Scaffold(
backgroundColor: Colors.transparent,
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter your e-mail";
}
String pattern = r'\w+@\w+\.\w+';
if (!RegExp(pattern).hasMatch(value)) {
return "Enter a Valid Email Address";
}
return null;
},
style: const TextStyle(color: Colors.white),
controller: _textController,
cursorColor: Colors.blue,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent)),
hintText: "E-Mail",
hintStyle: const TextStyle(color: Colors.white),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
onPressed: () {
_textController.clear();
},
icon: const Icon(
Icons.clear,
color: Colors.blue,
))),
),
const SizedBox(
height: 1,
),
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter your password";
}
String pattern =
r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~_-]).{8,}$';
if (!RegExp(pattern).hasMatch(value)) {
return 'Password must be least 8 character\n Password must include big-little letter and symbol';
}
return null;
},
style: const TextStyle(color: Colors.white),
//controller: _passwordController,
obscureText: !_showPassword,
cursorColor: Colors.blue,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent)),
hintText: "Password",
hintStyle: const TextStyle(color: Colors.white),
border: const OutlineInputBorder(),
suffixIcon: GestureDetector(
onTap: (() {
_togglevisibilitiy();
}),
child: Icon(
_showPassword
? Icons.visibility
: Icons.visibility_off,
color: Colors.blue,
),
)),
),
const SizedBox(height: 8),
GestureDetector(
onTap: () {
if (_formKey.currentState!.validate()) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HomePage()));
}
},
child: Container(
height: 40,
width: 90,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
color: Colors.lightBlueAccent
),
child: const Center(
child: Text(
"Log in",
style: TextStyle(color: Colors.white),
)),
),
)
],
),
),
),
),
);
}
}