Created
March 23, 2022 15:58
-
-
Save romanejaquez/9d8ba5b86a41bb08c91ae93cf35a3ffd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp( | |
MultiProvider( | |
providers: [ | |
Provider( | |
create: (_) => AppNotifyOnceProvider() | |
) | |
], | |
child: MyApp() | |
) | |
); | |
} | |
class AppNotifyOnceProvider { | |
bool showAlertAtInit = true; | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatefulWidget { | |
@override | |
MyWidgetState createState() => MyWidgetState(); | |
} | |
class MyWidgetState extends State<MyWidget> { | |
@override | |
Widget build(BuildContext context) { | |
AppNotifyOnceProvider appProvider = Provider.of<AppNotifyOnceProvider>(context, listen: false); | |
if(appProvider.showAlertAtInit) { | |
Future.delayed(const Duration(seconds: 1), () { | |
showDialog( | |
context: context, | |
builder: (context) { | |
return AlertDialog( | |
content: AppInitAlert() | |
); | |
}); | |
}); | |
} | |
return Scaffold( | |
appBar: AppBar(), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text('Hello World', textAlign: TextAlign.center, style: TextStyle(fontSize: 30)), | |
TextButton( | |
child: Text('Go to Page 2'), | |
onPressed: () { | |
Navigator.of(context).push( | |
MaterialPageRoute(builder: (context) => Page2()) | |
); | |
}, | |
) | |
] | |
) | |
); | |
} | |
} | |
class Page2 extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text('This is Page 2. Go back to the previous', style: TextStyle(fontSize: 30)), | |
TextButton( | |
onPressed: () { | |
Navigator.of(context).push( | |
MaterialPageRoute(builder: (context) => MyWidget()) | |
); | |
}, | |
child: Text("Click here to Push the First Page again, to try and trigger the alert") | |
) | |
] | |
) | |
); | |
} | |
} | |
class AppInitAlert extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: const EdgeInsets.all(30), | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text('By clicking the button below, it will make this dialog not appear anymore until you reload the whole app from scratch.'), | |
TextButton( | |
onPressed: () { | |
var appProvider = Provider.of<AppNotifyOnceProvider>(context, listen: false); | |
appProvider.showAlertAtInit = false; | |
Navigator.of(context).pop(); | |
}, | |
child: Text("Click here to Dismiss") | |
) | |
] | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment