this is my problem:
I have a Widget with a List View which contains a Text Field, at firs, when I tap the Text Field, it works perfect, however, If I tap the text Field, I write something, then I close the Text Field taping outside the keyboard, I navigate to another screen and I returned to the previous screen, the text field closes immediately.
How can give some problem If I only have a gesture detector, list view and a text Field ? Here is my code:
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:math';
import 'package:flutter/cupertino.dart' as cupertino;
import 'package:flutter/material.dart' hide BoxDecoration, BoxShadow;
import 'package:flutter/services.dart';
import 'package:flutter_animator/flutter_animator.dart';
import 'package:flutter_inset_box_shadow/flutter_inset_box_shadow.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:iknowApp/constant.dart';
import 'package:animated_theme_switcher/animated_theme_switcher.dart' as theme;
import 'package:like_button/like_button.dart';
import 'package:string_similarity/string_similarity.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../data/sentencesData.dart';
import '../data/sentencesDataEN.dart';
import '../data/sharedPreferences/sharedPreferences.dart';
import '../data/theme.dart';
import '../main.dart';
class QuizMode extends StatefulWidget {
const QuizMode({
Key key,
}) : super(key: key);
@override
State<QuizMode> createState() => _QuizModeState();
}
class _QuizModeState extends State<QuizMode> {
bool isPressed = false;
var result;
bool correct = true;
int contador = 0;
int quizScore = 0;
final controller = TextEditingController();
var list = sentences;
// generates a new Random object
var _random = new Random();
var element;
final formKey = GlobalKey();
FocusNode myFocusNode;
bool check = true;
bool like1 = true;
bool like2 = true;
bool like3 = true;
@override
void initState() {
// TODO: implement initState
super.initState();
element = list[_random.nextInt(list.length)];
myFocusNode = FocusNode();
}
@override
void dispose() {
myFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
Offset distance = isPressed
? Offset(MediaQuery.of(context).size.height * 0.01,
MediaQuery.of(context).size.height * 0.01)
: Offset(MediaQuery.of(context).size.height * 0.03,
MediaQuery.of(context).size.height * 0.03);
double blur = isPressed ? 5 : 30;
return !Constant.quizMode
? const MyHomePage()
: theme.ThemeSwitchingArea(
child: cupertino.GestureDetector(
onTap: () {
cupertino.FocusScope.of(context).unfocus();
},
child: Scaffold(
body: SafeArea(
child: cupertino.Center(
child: ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (context, index) {
final answ = element.answer[index];
final ques = element.question[index];
return cupertino.Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: contador < 3
? MediaQuery.of(context).size.height * 0.1
: MediaQuery.of(context).size.height * 0.05,
),
contador < 3
? cupertino.Column(
children: [
cupertino.Padding(
padding: EdgeInsets.only(
left: MediaQuery.of(context)
.size
.width *
0.1,
right: MediaQuery.of(context)
.size
.width *
0.1),
child: cupertino.Center(
child: TextField(
focusNode: myFocusNode,
enabled:
contador > 2 ? false : true,
key: formKey,
style: TextStyle(
color: Constant.isDarkMode
? cupertino.Color.fromARGB(
255, 179, 29, 29)
.withOpacity(0.7)
: cupertino.Color.fromARGB(
255, 109, 4, 4),
),
controller: controller,
// onChanged: onSearchTextChanged,
textAlign: TextAlign.left,
onSubmitted: (value) async {
print(result);
if (value != "") {
String answer = answ
.toString()
.toLowerCase();
result = controller.text
.toString()
.toLowerCase()
.similarityTo(answer);
if (result.toDouble() > 0.5) {
quizScore ;
if (quizScore >
Constant.score) {
Constant.score =
quizScore;
await DataPreferences
.setScore(
Constant.score);
}
setState(() {
contador = 0;
_random = Random();
element = list[
_random.nextInt(
list.length)];
correct = true;
controller.clear();
myFocusNode
.requestFocus();
});
} else {
setState(() {
correct = false;
contador ;
HapticFeedback
.heavyImpact();
if (contador == 1) {
like3 = false;
}
if (contador == 2) {
like2 = false;
}
if (contador == 3) {
like1 = false;
}
});
}
}
},
decoration: InputDecoration(
errorText: correct == false
? AppLocalizations.of(
context)
.incorrect
: null,
enabledBorder:
UnderlineInputBorder(
borderSide: BorderSide(
color: Constant.isDarkMode
? cupertino.Color
.fromARGB(255,
97, 55, 55)
.withOpacity(0.7)
: cupertino.Color
.fromARGB(
255, 77, 6, 6),
),
),
hintText:
AppLocalizations.of(context)
.answer,
hintStyle: TextStyle(
fontSize:
MediaQuery.of(context)
.size
.height *
0.02,
fontWeight: FontWeight.normal,
color: Constant.isDarkMode
? cupertino.Color
.fromARGB(255,
151, 111, 111)
.withOpacity(0.7)
: cupertino.Color
.fromARGB(
255, 59, 31, 104),
),
focusedBorder: InputBorder.none,
),
),
),
),
],
)
: cupertino.Center(),
theme.ThemeSwitcher(builder: (context) {
return InkWell(
onTap: () {
setState(() {
Constant.bodyAnimation = true;
Constant.quizMode = false;
});
},
child:
Text(AppLocalizations.of(context).returnn,
style: GoogleFonts.alike(
textStyle: Theme.of(context)
.textTheme
.displayMedium,
fontSize: MediaQuery.of(context)
.size
.width *
0.04,
color: Constant.isDarkMode
? cupertino.Color.fromARGB(
255, 185, 51, 51)
.withOpacity(0.7)
: cupertino.Color.fromARGB(
255, 158, 158, 158),
)),
);
}),
],
),
);
}),
),
),
),
));
}
}
CodePudding user response:
I think this piece of code is making it lose the focus on text field which is why keyboard is closed immediately
onTap: () {
cupertino.FocusScope.of(context).unfocus();
},
CodePudding user response:
I finally solved my problem, changing:
final formKey = GlobalKey();
FocusNode myFocusNode;
to static final, as you can see here;
static final formKey = GlobalKey();
static final myFocusNode = FocusNode();
and works without any problem.