Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textformfield reloads screen when focused after navigation

Tags:

flutter

... 
 @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'haha',
      theme: ThemeData(
          fontFamily: 'Iransans',
          primaryColor: Palette.primaryColor,
          canvasColor: Palette.primaryColor, //my custom color
          accentColor: Palette.accentColor),
      initialRoute: "/",
      routes: {
        "/": (context) => Directionality(
            textDirection: TextDirection.rtl, child: SplashPage()),
... 

and in SplashPage, I navigate from here to login page


  @override
  Widget build(BuildContext context) {
    Future.delayed(const Duration(seconds: 4), () {
       Navigator.pushNamed(context,"/login" /*,arguments: "free"*/);
    });
    return const Scaffold(
      backgroundColor: Palette.white,
      body: Center(
        child:
            SizedBox(height: 60, width: 60, child: CircularProgressIndicator()),
      ),
    );
  }

here is my TextField in LoginPage. the page that doesn't work but when navigating here from MainPage directly everything is well

Scaffold(
      backgroundColor: Palette.primaryColor,
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
           TextField(
                autofocus: true,
                textAlign: TextAlign.center,
                //controller: _textFieldControllerUserName,
                keyboardType: TextInputType.name,
            ),
          ],
        ),
      ),
    );
like image 382
Atras VidA Avatar asked Dec 06 '25 13:12

Atras VidA


1 Answers

flutter keeps pages in a stack and the whole thing in the stack is working :( build method runs even when the page is in backStack, so I moved my navigator to initState()

class SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(seconds: 4), () {
      _navigateLoginPage(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      backgroundColor: Palette.white,
      body: Center(
        child:
            SizedBox(height: 60, width: 60, child: CircularProgressIndicator()),
      ),
    );
  }
like image 66
Atras VidA Avatar answered Dec 08 '25 06:12

Atras VidA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!