-4

If a user click on the "Yes, I'm 18 above" then only user proceed forward. Otherwise stop there only. I want to make this in starting of application when application start in flutter language. So you can help me in this?

Rohan Thacker
  • 5,377
  • 3
  • 21
  • 34
  • https://stackoverflow.com/questions/61249772/how-to-make-age-validation-in-flutter - Your answer can be here – Nijat Namazzade Oct 04 '22 at 11:44
  • Does this answer your question? [How to make age validation in flutter](https://stackoverflow.com/questions/61249772/how-to-make-age-validation-in-flutter) – Joachim Sauer Oct 04 '22 at 11:56

3 Answers3

0

We have many differents methods to do this...

You can try this...

In your Main.dart :

///Create this method
void checkAge(){
    if (isSwitched){
      /// put your navigation code here
    } else {
      exit(0);   /// in this example if not checked the app will be close.
    }
  }


/// call the method on ui.
SwitchListTile.adaptive(
              dense: true,
              title: const Text(
                '+ 18 age ?',
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: Colors.red,
                ),
              ),
              controlAffinity: ListTileControlAffinity.platform,
              contentPadding: EdgeInsets.zero,
              value: isSwitched,
              activeColor: Theme.of(context).colorScheme.secondary,
              onChanged: (bool value) {
                setState(() {
                  isSwitched = value;
                });
              },
            ),
            TextButton(
              autofocus: false,
              onPressed: () => checkAge(),
              child: const Text('Enter'),  
            )

Edit ...

For popup ...

Create an AlertDialog Class in your main :

Future<bool> showAlertDialog(final BuildContext context) async{
 
  bool isSwitched = false;
  return await showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) {
        return StatefulBuilder(
            builder: (context, setState) {
          return AlertDialog (
            backgroundColor: Colors.white,
            title: SizedBox(
              width: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text(
                    'Check',
                    style: TextStyle(
                      color: Colors.red,
                    ),
                  )
                ],
              ),
            ),
            content: SwitchListTile.adaptive(
                dense: true,
                title: const Text(
                  '+ 18 age ?',
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: Colors.red,
                  ),
                ),
                controlAffinity: ListTileControlAffinity.platform,
                contentPadding: EdgeInsets.zero,
                value: isSwitched,
                activeColor: Theme.of(context).colorScheme.secondary,
                onChanged: (bool value) {                
                  setState(()=>{isSwitched = value});                   
                },
              ),            
            actions: <Widget>[
              SizedBox(
                width: 125,
                child: TextButton(                                
                  onPressed: () {
                    do{
                      Navigator.of(context, rootNavigator: true).pop(isSwitched);
                    } while (1==2);

                  },
                  child: const Text(
                    'Check Age',
                    style: TextStyle(
                      color: Colors.red,
                    ),
                  ),
                ),
              ),
            ],
          );
        });
  });
}

///And change to call :
void checkAge() async{
    final bool checked = await showAlertDialog(context);
    
    if (checked){
      print('ok');
    } else {
      print('not ok');
    }
  }

TextButton(
              autofocus: false,
              onPressed: () => getAlert(context),
              child: const Text('Enter'),  
            )
Sergio Clemente
  • 809
  • 8
  • 12
0

As far I understand you want to make a pop up dialog at the starting of app. If user say I am 18+ then simply continue the process, if user say no, then close the app. To do this first create a dialog. then call it first screen's initstate.

    void ageConfirmationDialog() async {
    await showDialog(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Age Alert'),
          content: const Text('Are you 18+?'),
          actions: <Widget>[
            TextButton(
              child: const Text('No'),
              onPressed: () {
                SystemNavigator.pop();
                // close the app
              },
            ),
            TextButton(
              child: const Text('Yes, I\'m 18 above'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    );
  }

Now call this method in initState

    @override
  void initState() {
    ageConfirmationDialog();
    super.initState();
  }
AFRIDI
  • 126
  • 4
-1

You could use an Alert Dialogue

    actions: <Widget>[  
        title:text("User above 18"), 
        TextButton(child: Text("Yes"),
          ontapped(){
            //code to route page here
           }),  
        TextButton(child: Text("No"),)  
    ])