I'm having trouble with my TextField widgets. Every time the user taps on them, the whole widget reloads, and the keyboard disappears. I believe I tracked the problem down to a streambuilder I have on main checking for user authentication. Here's a summarized version of my MyApp widget in my main.dart:
class MyApp extends StatelessWidget {
const MyApp({super.key});
// this widget is the root of your application.
@override
Widget build(BuildContext context) {
SizeConfig.init(context);
final auth = FirebaseAuth.instance;
return MultiProvider(
providers: [
//Providers go in here...
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'livit',
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: mobileBackgroundColor,
),
home: StreamBuilder(
stream: auth.authStateChanges(),
builder: (context, snapshot) {
//over here we have logic to go to the login screen or the main page
},
),
),
);
}
}
Once the app checks if the user's authenticated, it either takes them to the login screen or the main page. And that works like a charm. Unfortunately, the user tries to tap on any text field, they can't type, because as soon as they focus, the widget reloads.
So I made a clean flutter project, and cooked up a widget to test if everything went fine:
class TextInputTest extends StatefulWidget {
const TextInputTest({super.key, required this.title});
final String title;
@override
State<TextInputTest> createState() => _TextInputTestState();
}
class _TextInputTestState extends State<TextInputTest> {
final TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: TextField(
controller: _controller,
),
),
);
}
}
And it worked, which is neat! I want to note that all of my text-field-using screens are formatted similarly, as in they are stateful and the controller is declared inside the state.
Now, if I plop my perfectly working test widget where the stream builder usually is, the widget works just fine. However, if I replace either the login screen or the main page, the test widget has the same exact problem, where the whole widget will reload on focus and the user will never be able to pull up the screen.
Can you please help me out to solve this problem? I sort of need my users to be able to type stuff.
Thanks in advance for any help provided, and please let me know if you need more information!
PS: I did check this thread out, and none of the answers seemed to be fruitful.
const
before login screen and main page? For example:const LoginScreen()
– Thierrystream: auth.authStateChanges(),
. Do not make the stream in the stream: parameter of a StreamBuilder. See youtu.be/sqE-J8YJnpg for details. – MelvamelvenaStreamBuilder
in a variable and then try using the variable in place of their instances ! This trick saved me a lot of time !! – Wholesale