Files
Regroup/lib/Feed Module/Main_Screens/ProfileTab/Settings/View/Notification.dart
2024-07-15 19:27:16 +05:30

173 lines
5.3 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:regroup/Common/CommonWidget.dart';
import 'package:regroup/Common/base_manager.dart';
import 'package:regroup/Feed%20Module/Main_Screens/ProfileTab/Settings/ViewModel/NotificationApi.dart';
import 'package:regroup/Utils/Common/CommonAppbar.dart';
import 'package:regroup/Utils/Common/blureffect.dart';
import 'package:regroup/Utils/Common/sized_box.dart';
import 'package:regroup/Utils/dialogs.dart';
import 'package:regroup/Utils/texts.dart';
class NotificationScreen extends StatefulWidget {
const NotificationScreen({super.key});
@override
State<NotificationScreen> createState() => _NotificationScreenState();
}
class _NotificationScreenState extends State<NotificationScreen> {
var switchValues = List<bool>.generate(5, (index) => false).obs;
void toggleSwitch(int index, bool value) {
switchValues[index] = value;
}
String switchValueFinal(int index) {
return switchValues[index] ? '1' : '0';
}
void switchValueBool() {
final notifications = [
fetchNoti!.data!.groupNotification,
fetchNoti!.data!.communityNotification,
fetchNoti!.data!.followerNotification,
fetchNoti!.data!.newFollowerNotification,
fetchNoti!.data!.directMessageNotification,
];
for (int i = 0; i < notifications.length; i++) {
toggleSwitch(i, notifications[i] == 1);
}
}
Future<void> updateNotification() async {
Map<String, String> updata = {
"group_notification": switchValueFinal(0),
"community_notification": switchValueFinal(1),
"follower_notification": switchValueFinal(2),
"new_follower_notification": switchValueFinal(3),
"direct_message_notification": switchValueFinal(4),
};
final data = await NotificationAPI().notificationPostApi(updata);
if (data.status == ResponseStatus.SUCCESS) {
utils.showToast('Notification updated');
} else {
Get.snackbar(
"Error!",
data.data['message'],
duration: Duration(seconds: 2),
colorText: Colors.white,
backgroundColor: Colors.red,
margin: EdgeInsets.all(8),
snackStyle: SnackStyle.FLOATING,
snackPosition: SnackPosition.BOTTOM,
);
}
}
late Future myfuture;
@override
void initState() {
myfuture = NotificationAPI().getNotification();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Color(0xFF222935),
extendBody: true,
appBar: CommonAppbar(
titleTxt: "Notifications",
),
body: FutureBuilder(
future: myfuture,
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(
color: Colors.blue,
),
);
}
if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18.spMin),
),
);
}
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
switchValueBool();
return Stack(
children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill,
),
),
),
SingleChildScrollView(
child: Column(
children: [
sizedBoxHeight(20.h),
rowTile(text: "Group notification", index: 0),
commonDivider(),
rowTile(text: "Community notification", index: 1),
commonDivider(),
rowTile(text: "Follower notification", index: 2),
commonDivider(),
rowTile(text: "New follower notification", index: 3),
commonDivider(),
rowTile(text: "Direct message notification", index: 4),
],
),
),
],
);
}
return Container();
},
),
);
}
Widget rowTile({required String text, required int index}) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.h),
child: Row(
children: [
text16w400_FCFCFC(text),
Spacer(),
Obx(
() => Transform.scale(
scaleY: 1,
child: CupertinoSwitch(
value: switchValues[index],
trackColor: Colors.white.withOpacity(0.4),
activeColor: Color(0xFF34C759),
onChanged: (bool? value) {
toggleSwitch(index, value ?? false);
updateNotification();
},
),
),
),
],
),
);
}
}