Created
April 30, 2020 13:13
-
-
Save GursheeshSingh/2a889f35d35e67f129d33bf12dd82912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'package:talkfootball/constants.dart'; | |
class DoNotAskAgainDialog extends StatefulWidget { | |
final String title, subTitle, positiveButtonText, negativeButtonText; | |
final Function onPositiveButtonClicked; | |
final String doNotAskAgainText; | |
final String dialogKeyName; | |
DoNotAskAgainDialog( | |
this.dialogKeyName, | |
this.title, | |
this.subTitle, | |
this.positiveButtonText, | |
this.negativeButtonText, | |
this.onPositiveButtonClicked, { | |
this.doNotAskAgainText = 'Never ask again', | |
}); | |
@override | |
_DoNotAskAgainDialogState createState() => _DoNotAskAgainDialogState(); | |
} | |
class _DoNotAskAgainDialogState extends State<DoNotAskAgainDialog> { | |
bool doNotAskAgain = false; | |
_updateDoNotShowAgain() async { | |
SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); | |
await sharedPreferences.setBool(kUpdateDialogKeyName, false); | |
} | |
@override | |
Widget build(BuildContext context) { | |
if (Platform.isIOS) { | |
return CupertinoAlertDialog( | |
title: Text(widget.title), | |
content: Text(widget.subTitle), | |
actions: <Widget>[ | |
CupertinoDialogAction( | |
child: Text( | |
widget.positiveButtonText, | |
), | |
onPressed: widget.onPositiveButtonClicked, | |
), | |
CupertinoDialogAction( | |
child: Text( | |
widget.doNotAskAgainText, | |
), | |
onPressed: () { | |
Navigator.pop(context); | |
_updateDoNotShowAgain(); | |
}, | |
), | |
CupertinoDialogAction( | |
child: Text( | |
widget.negativeButtonText, | |
), | |
onPressed: () => Navigator.pop(context), | |
), | |
], | |
); | |
} | |
return AlertDialog( | |
title: Text( | |
widget.title, | |
style: TextStyle(fontSize: 24), | |
), | |
content: FittedBox( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Text(widget.subTitle), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.start, | |
children: <Widget>[ | |
SizedBox( | |
width: 24, | |
height: 24, | |
child: Checkbox( | |
value: doNotAskAgain, | |
onChanged: (val) { | |
setState(() { | |
doNotAskAgain = val; | |
}); | |
}, | |
), | |
), | |
SizedBox(width: 8), | |
GestureDetector( | |
onTap: () { | |
setState(() { | |
doNotAskAgain = doNotAskAgain == false; | |
}); | |
}, | |
child: Text( | |
widget.doNotAskAgainText, | |
style: TextStyle(color: kDarkGray), | |
), | |
), | |
], | |
) | |
], | |
), | |
), | |
actions: <Widget>[ | |
FlatButton( | |
child: Text(widget.positiveButtonText), | |
onPressed: doNotAskAgain ? null : widget.onPositiveButtonClicked, | |
), | |
FlatButton( | |
child: Text( | |
widget.negativeButtonText, | |
style: TextStyle(color: Colors.red), | |
), | |
onPressed: () async { | |
Navigator.pop(context); | |
if (doNotAskAgain) { | |
_updateDoNotShowAgain(); | |
} | |
}, | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment