I’m having trouble with the text fields on my TV. When I try to enter my email, password, and then click the button, the focus jumps around unexpectedly. It seems like the keyboard is automatically appearing and focusing on the button when I press keys. I’ve tried everything I can think of, but the focus is still not working correctly
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:searchrtv/widgets/customTextFormField.dart';
import 'package:searchrtv/widgets/custombtn.dart';
class Login extends StatelessWidget {
Login({super.key});
final TextEditingController emailController = TextEditingController();
final Rx<bool> isObscure = true.obs;
final loginFormKey = GlobalKey<FormState>();
final TextEditingController passwordController = TextEditingController();
FocusNode? emailFocusNode;
FocusNode? passwordFocusNode;
FocusNode? btnFocusNode;
@override
Widget build(BuildContext context) {
emailFocusNode = FocusNode(
onKey: (node, event) {
node.unfocus();
if (event.isKeyPressed(LogicalKeyboardKey.arrowUp)) {
emailFocusNode!.unfocus();
btnFocusNode!.requestFocus();
return KeyEventResult.handled;
} else if (event.isKeyPressed(LogicalKeyboardKey.arrowDown)) {
emailFocusNode!.unfocus();
passwordFocusNode!.requestFocus();
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
);
passwordFocusNode = FocusNode(
onKey: (node, event) {
if (event.isKeyPressed(LogicalKeyboardKey.arrowUp)) {
passwordFocusNode!.unfocus();
emailFocusNode!.requestFocus();
return KeyEventResult.handled;
} else if (event.isKeyPressed(LogicalKeyboardKey.arrowDown)) {
passwordFocusNode!.unfocus();
btnFocusNode!.requestFocus();
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
);
btnFocusNode = FocusNode(
onKey: (node, event) {
if (event.isKeyPressed(LogicalKeyboardKey.arrowUp)) {
btnFocusNode!.unfocus();
passwordFocusNode!.requestFocus();
return KeyEventResult.handled;
} else if (event.isKeyPressed(LogicalKeyboardKey.arrowDown)) {
btnFocusNode!.unfocus();
emailFocusNode!.requestFocus();
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
);
final size = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: size.height * 1,
width: size.width * 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
width: size.width * 0.4,
height: size.height * 0.5,
color: Colors.grey[900],
child: Form(
key: loginFormKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 10),
const Center(
child: Icon(
Icons.remove_red_eye_rounded,
size: 64,
),
),
const Text(
'Searchr.Tv',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 10),
customTextFormField(
focusNode: emailFocusNode,
labelText: 'Email',
hintText: 'Email Address',
prefixIconData: Icons.email,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
validator: (value) {
return null;
},
),
const SizedBox(height: 10.0),
customTextFormField(
focusNode: passwordFocusNode,
labelText: 'Password',
hintText: "****",
prefixIconData: Icons.vpn_key_rounded,
textInputAction: TextInputAction.next,
validator: (value) {
return null;
},
),
const SizedBox(height: 6.0),
CustomButton(
focusNode: btnFocusNode,
text: 'Login',
onPressed: () async {
},
hasInfiniteWidth: true,
),
],
),
),
),
const Divider(
height: 200,
color: Colors.black,
),
const SizedBox(width: 10),
Container(
width: size.width * 0.4,
height: size.height * 0.4,
color: Colors.green,
child: const Text("data"),
),
],
),
),
);
}
}
New contributor
Najeeb Ullah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.