How to display iOS/cupertino alert dialog in Android using Flutter?

后端 未结 4 2241
野性不改
野性不改 2021-01-04 13:18

I was trying to display a iOS themed dialog box in my Flutter app, but I was unable to find anything in the docs

4条回答
  •  一个人的身影
    2021-01-04 13:54

    The following is a simple example of how to create a simple alert with two button in Flutter,

    import a import 'package:flutter/cupertino.dart'; and Copy and paste below code and Call it showAlertDialog(context); where you want to show Dialog.

    void showAlertDialog(BuildContext context) {
    
      showDialog(
        context: context,
        child:  CupertinoAlertDialog(
          title: Text("Log out?"),
          content: Text( "Are you sure you want to log out?"),
          actions: [
            CupertinoDialogAction(
                isDefaultAction: true,
                onPressed: (){
                  Navigator.pop(context);
                },
                child: Text("Cancel")
            ),
            CupertinoDialogAction(
              textStyle: TextStyle(color: Colors.red),
                isDefaultAction: true,
                onPressed: () async {
                  Navigator.pop(context);
                  SharedPreferences prefs = await SharedPreferences.getInstance();
                  prefs.remove('isLogin');
                  Navigator.pushReplacement(context,
                      MaterialPageRoute(builder: (BuildContext ctx) => LoginScreen()));
                },
                child: Text("Log out")
            ),
          ],
        ));
    }
    

提交回复
热议问题