Open Alert when user enters access screen

Asked

Viewed 26 times

0

I needed as soon as the user accessed a particular application screen to open a alertDialog. But I can’t make it happen. I’ve tried opening with initState but it still won’t open.

I have this modal, it works if triggered by a button.

Future<void> clienteEndereco() async {
    return showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Center(
              child: Text(
                'Complete seu cadastro',
                style: TextStyle(fontWeight: FontWeight.w800),
              )),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(bottom: 30.0),
                  child: Text(
                    "Para usar todos os benefícios do Clube Mercosul complete seu cadastro",

                    style: TextStyle(fontWeight: FontWeight.w500),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Expanded(
                      flex: 1,
                      child: InkWell(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: Text(
                            "Cancelar",
                            style: TextStyle(
                                fontSize: 18,
                                color: blueColor,
                                fontWeight: FontWeight.w800),
                          )),
                    ),
                    Expanded(
                      flex: 1,
                      child: InkWell(
                          onTap: () {


                          },
                          child: Text(
                            "Completar meu cadastro",
                            style: TextStyle(
                                fontSize: 18,
                                color: blueColor,
                                fontWeight: FontWeight.w800),
                          )),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

I’ve tried calling him a builder but not so much.

  _HomePageState() {
    clienteEndereco();//alerta
  }

2 answers

0

Call the clientEndereco() right after redirecting to the screen you want. For example, according to the example of the flutter.

onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),
  );
 clienteEndereco()
}

So the moment you are redirected to the next screen, in the "Secondroute" example, customer contentEndereco() will be displayed!

0

You can use the initState() of Statefulwidget, only need to wait for the screen to be built, to have the correct access to the context of the same.

Below is an example of how it can be done:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: YellowBird(),
        ),
      ),
    );
  }
}

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

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

class _YellowBirdState extends State<YellowBird> {
  
  @override
  initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 100)).then((_) {
      showDialog(
        context: context, 
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text("My Super title"),
            content: Text("Hello World"),
          );
        }
      );
    });
  }
                 
  @override
  Widget build(BuildContext context) {
    return Container(color: const Color(0xFFFFE306));
  }
}

You can test on Dartpad

Note: You can also use the Layoutbuilder or even the Futurebuilder to gain access to the context of the screen when trying to display the Alertdialog

Browser other questions tagged

You are not signed in. Login or sign up in order to post.