mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
refactor: split app wrapper from focused example code (single-file apps)
Convert 20 more single-file apps to the validated pattern: lib/main.dart holds only the runApp + MaterialApp bootstrap, while a new lib/example.dart holds the full screen (Scaffold, AppBar) and the focused example code. Apps: enabling_splash_screen, getx_counter_app, image_from_network, infinite_list, load_local_image, load_local_json, persist_key_value, push_notifications, sliver_app_bar_example, stateless_widgets, tic_tac_toe, tip_calculator, using_alert_dialog, using_edittext, using_expansionpanel, using_http_get, using_interactiveviewer, using_snackbar, using_stepper, using_theme
This commit is contained in:
130
push_notifications/lib/example.dart
Normal file
130
push_notifications/lib/example.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Example: receive and display Firebase Cloud Messaging push notifications.
|
||||
class Example extends StatefulWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
State<Example> createState() => _ExampleState();
|
||||
}
|
||||
|
||||
class _ExampleState extends State<Example> {
|
||||
String title = "Title will appear here";
|
||||
String messageData = "Message text will appear here";
|
||||
|
||||
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
|
||||
|
||||
@override
|
||||
Future<void> initState() async {
|
||||
super.initState();
|
||||
|
||||
/*
|
||||
Message that we are getting from firebase will be of form:-
|
||||
{
|
||||
notification:{
|
||||
title:'Custom Title that we send from firebase cloud messaging',
|
||||
body:'Text of the message will appear here'
|
||||
},
|
||||
data:{
|
||||
Here the extra data like if we include image,etc optional data from firebase cloud messaging
|
||||
}
|
||||
}
|
||||
|
||||
For sending Push notification go to Grow and then cloud messaging, from there send new message by adding title
|
||||
and other fields as per requirement
|
||||
|
||||
*/
|
||||
// Request permission (iOS) and listen for messages.
|
||||
await _firebaseMessaging.requestPermission(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
setState(() {
|
||||
title = message.notification?.title ?? title;
|
||||
messageData = message.notification?.body ?? messageData;
|
||||
notification(context, title, messageData);
|
||||
});
|
||||
});
|
||||
|
||||
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
||||
setState(() {
|
||||
title = message.notification?.title ?? title;
|
||||
messageData = message.notification?.body ?? messageData;
|
||||
notification(context, title, messageData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Push Notification Demo'),
|
||||
backgroundColor: Colors.green,
|
||||
centerTitle: true,
|
||||
),
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 30.0),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
SizedBox(
|
||||
height: 20.0,
|
||||
),
|
||||
Text(
|
||||
messageData,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// this function will be called when a push notification is recieved and show as alert dialog along with
|
||||
// title and message body
|
||||
Future notification(
|
||||
BuildContext context, String title, String messageText) async {
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
buttonPadding: const EdgeInsets.all(10.0),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20.0)),
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold, fontSize: 20.0),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15.0,
|
||||
),
|
||||
Text(
|
||||
messageText,
|
||||
style: const TextStyle(fontSize: 16.0),
|
||||
)
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Ok'))
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
@@ -14,132 +17,7 @@ class MyApp extends StatelessWidget {
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: MyHomePage(),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key});
|
||||
|
||||
@override
|
||||
_MyHomePageState createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
String title = "Title will appear here";
|
||||
String messageData = "Message text will appear here";
|
||||
|
||||
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
|
||||
|
||||
@override
|
||||
Future<void> initState() async {
|
||||
super.initState();
|
||||
|
||||
/*
|
||||
Message that we are getting from firebase will be of form:-
|
||||
{
|
||||
notification:{
|
||||
title:'Custom Title that we send from firebase cloud messaging',
|
||||
body:'Text of the message will appear here'
|
||||
},
|
||||
data:{
|
||||
Here the extra data like if we include image,etc optional data from firebase cloud messaging
|
||||
}
|
||||
}
|
||||
|
||||
For sending Push notification go to Grow and then cloud messaging, from there send new message by adding title
|
||||
and other fields as per requirement
|
||||
|
||||
*/
|
||||
// Request permission (iOS) and listen for messages.
|
||||
await _firebaseMessaging.requestPermission(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
setState(() {
|
||||
title = message.notification?.title ?? title;
|
||||
messageData = message.notification?.body ?? messageData;
|
||||
notification(context, title, messageData);
|
||||
});
|
||||
});
|
||||
|
||||
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
||||
setState(() {
|
||||
title = message.notification?.title ?? title;
|
||||
messageData = message.notification?.body ?? messageData;
|
||||
notification(context, title, messageData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Push Notification Demo'),
|
||||
backgroundColor: Colors.green,
|
||||
centerTitle: true,
|
||||
),
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 30.0),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
SizedBox(
|
||||
height: 20.0,
|
||||
),
|
||||
Text(
|
||||
messageData,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// this function will be called when a push notification is recieved and show as alert dialog along with
|
||||
// title and message body
|
||||
Future notification(
|
||||
BuildContext context, String title, String messageText) async {
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
buttonPadding: EdgeInsets.all(10.0),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20.0)),
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
|
||||
),
|
||||
SizedBox(
|
||||
height: 15.0,
|
||||
),
|
||||
Text(
|
||||
messageText,
|
||||
style: TextStyle(fontSize: 16.0),
|
||||
)
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context), child: Text('Ok'))
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user