Home > Software design >  Alert dialog reloads every time the textfield is clicked
Alert dialog reloads every time the textfield is clicked

Time:10-20

I am including an AlertDialog when the app user is not registered in Cloud Firestore:

class MuroScreen extends StatefulWidget {
  const MuroScreen({Key? key}) : super(key: key);

  @override
  _MuroScreenState createState() => _MuroScreenState();
}

class _MuroScreenState extends State<MuroScreen> {

  CollectionReference userRef = FirebaseFirestore.instance.collection('clientes');

  Future<void> salir() async {
    await FirebaseAuth.instance.signOut().catchError((error){
      print(error.toString());
    });
    Navigator.of(context).push(MaterialPageRoute(builder: (c)=>MyHomePage001()),);
  }

  Future<void> verificarUsuario() async{
    DocumentSnapshot snapshotUser = await userRef
        .doc(FirebaseAuth.instance.currentUser!.phoneNumber)
        .get();

    print("existe?" snapshotUser.exists.toString());
    if(snapshotUser.exists){
      //ya existe
      print("si  EXISTE");
    }
    else {
      //no existe
      print("no  EXISTE");
      var nameController = TextEditingController();
      Alert(
          context: context,
          title: "LOGIN",
          content: Column(
            children: <Widget>[
              TextField(
                decoration: InputDecoration(
                  icon: Icon(Icons.account_circle),
                  labelText: 'Username',
                ),
              ),
              TextField(
                obscureText: true,
                decoration: InputDecoration(
                  icon: Icon(Icons.lock),
                  labelText: 'Password',
                ),
              ),
            ],
          ),
          buttons: [
            DialogButton(
              onPressed: () => Navigator.pop(context),
              child: Text(
                "LOGIN",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            )
          ]).show();


    }


  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    verificarUsuario();

  }

  @override
  Widget build(BuildContext context) {



    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            margin: EdgeInsets.all(65),
            width: double.infinity,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: Colors.red,
              ),
              onPressed: ()  {
                salir();


              },
              child: Text("Logout",style: TextStyle(color:Colors.white,fontSize: 16, fontWeight: FontWeight.bold),),
            ),
          )

        ],

      ),
    );
  }
}

Alert is a rflutter_alert widget.

The issue is that every time I click on a textfield inside the alert widget, the screen reloads and shows the alert again without showing the keyboard, it reloads again.

CodePudding user response:

You are calling the verificarUsuario(); in init() which doesn't have a context. The alert needs a context from build method to show. Please add a button inside the scaffold and open this alert after passing the the context as an argument. It should work.

  • Related