labTask12
labTask12
import 'package:flutter/material.dart';
import "package:fluttertoast/fluttertoast.dart";
class GetToastNotification {
static void showToastNotiction(String info_disp) {
Fluttertoast.showToast(
msg: info_disp,
89
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 60, 24,
161),
textColor: Colors.white,
fontSize: 16.0,
);
}
}
main .dart
import 'package:flutter/material.dart';
import 'package:notifications/notification_file
/Toast_notificatio.dart';
void main() {
runApp(const MaterialApp(
home: Notification(),
debugShowCheckedModeBanner: false,
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
90
onPressed: () {
GetToastNotification.showToastNotiction(
"Hi this is toast notification");
},
child: Text('Show Toast notification'),
),
),
);
}
}
After the above code run After show toast button clicked
The above toast notification defines once in Toast_notificatio.dart file and you can use in any place through
your application screens.
91
To do Local notification task please follow the following step: -
continue with the above application
add flutter_local_notifications dependency to pubspec.yaml next to fluttertoast
to get the latest version you can visit pub.dev
local_notification.dart
import
'package:flutter_local_notifications/flutter_local_notif
ications.dart';
class GetLocalNotification {
static final FlutterLocalNotificationsPlugin
flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
92
AndroidInitializationSettings('@mipmap/ic_launch
er');
await
flutterLocalNotificationsPlugin.initialize(initializatio
nSettings);
}
93
await flutterLocalNotificationsPlugin.show(
0,
title,
info_disp,
platformChannelSpecifics,
payload: 'id',
);
}
}
import 'package:flutter/material.dart';
import
'package:notifications/notification_file/Toast_notificat
io.dart';
import
'package:notifications/notification_file/local_notificat
ion.dart';
94
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
GetToastNotification.showToastNotiction(
"Hi this is toast notification");
},
child: Text('Show Toast notification'),
),
SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () {
GetLocalNotification.showNotification(
"Add student", "one student is
added");
},
child: Text('Show Local notification'),
),
],
)),
);
}
}
95
After run the above code After show local notification button clicked
96
After swiping down the phone top screen
Exercise: apply this local and toast notification on previous task to get new notification when student
add, delete and update.
Your expected screens are bellow
97
Add new student (add Update student data After swiping down the After expanding the
button clicked) ( update button clicked phone student_info notification
Happy codding !!
98