Skip to content

Commit

Permalink
fixed plugin issue working on implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
coderrrrr2 committed Jan 15, 2024
1 parent 6b7071f commit 5242c05
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
package com.example.to_do_app

import android.content.ContentResolver
import android.content.Context
import android.media.RingtoneManager
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.util.*

class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "example.dev/to_do_app").setMethodCallHandler { call, result ->
if ("drawableToUri" == call.method) {
val resourceId = this@MainActivity.resources.getIdentifier(call.arguments as String, "drawable", this@MainActivity.packageName)
result.success(resourceToUriString(this@MainActivity.applicationContext, resourceId))
}
if ("getAlarmUri" == call.method) {
result.success(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString())
}
if ("getTimeZoneName" == call.method) {
result.success(TimeZone.getDefault().id)
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ImagePickerPlugin.registerWith(registrarFor("io.flutter.plugins.imagepicker.ImagePickerPlugin"))
SqflitePlugin.registerWith(registrarFor("com.tekartik.sqflite.SqflitePlugin"))
}

private fun resourceToUriString(context: Context, resId: Int): String? {
return (ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ context.resources.getResourcePackageName(resId)
+ "/"
+ context.resources.getResourceTypeName(resId)
+ "/"
+ context.resources.getResourceEntryName(resId))
}
}
12 changes: 4 additions & 8 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import UIKit
import Flutter
import flutter_local_notifications
import flutter_timezone

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {

override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

// Set up the plugin registrant callback for FlutterLocalNotificationsPlugin
FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry)
}

// Your other setup code...

// Set up the plugin registrant callback for FlutterLocalNotificationsPlugin
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
2 changes: 1 addition & 1 deletion ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Habit Tracker App</string>
<string>ToDo App</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:to_do_app/services/local%20Notifications/local_notifications.dart';
import 'package:to_do_app/services/local%20Notifications/local_notifications_initialiser.dart';
import 'package:to_do_app/theme/color_scheme.dart';
import 'package:to_do_app/providers/settings_provider.dart';
import 'package:to_do_app/screens/home.dart';

void main() async {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
LocalNotificationsService().initialiseTimeZones();
LocalNotificationsInitializer().initialisePlugin();
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
runApp(const ProviderScope(child: ToDoApp()));
FlutterNativeSplash.remove();
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/settings_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SettingsProvider extends StateNotifier<Settings> {

final settingsProvider =
StateNotifierProvider<SettingsProvider, Settings>((ref) {
Settings initialSettings = Settings(
Settings initialSettings = const Settings(
isNotifications: true,
isLightMode: false,
chosenLanguage: 'Select Language',
Expand Down
80 changes: 36 additions & 44 deletions lib/screens/add_new_todo.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:multi_dropdown/multiselect_dropdown.dart';
import 'package:to_do_app/models/to_do.dart';
import 'package:to_do_app/providers/settings_provider.dart';
import 'package:to_do_app/providers/to_do_provider.dart';
import 'package:to_do_app/screens/set_repeat_task_screen.dart';

final formatter = DateFormat.yMd();

Expand All @@ -16,24 +16,14 @@ class TasksScreen extends ConsumerStatefulWidget {
}

class _TasksScreenState extends ConsumerState<TasksScreen> {
final MultiSelectController<String> _controller = MultiSelectController();
List<String> _selectedOptions = [];

List<String> repeatDaysChoices = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Everyday",
];

final formKey = GlobalKey<FormState>();
final taskController = TextEditingController();
DateTime? enteredDate;
bool textformKeyIsEmpty = true;
bool isDateSelected = false;
TimeOfDay? selectedTime;
String? repeatDays;
String repeatDays = "No";
Color darkHeaderColour = const Color.fromARGB(255, 156, 81, 231);

void showDatePickerDialog() async {
Expand Down Expand Up @@ -95,7 +85,6 @@ class _TasksScreenState extends ConsumerState<TasksScreen> {
@override
Widget build(BuildContext context) {
final theme = ref.watch(settingsProvider);
repeatDays = _selectedOptions.join();
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
Expand Down Expand Up @@ -256,36 +245,39 @@ class _TasksScreenState extends ConsumerState<TasksScreen> {
),
],
),
Container(
margin: const EdgeInsets.all(15),
child: MultiSelectDropDown<String>(
hint: 'Select Day/Days',
dropdownHeight: 250,
showClearIcon: true,
controller: _controller,
onOptionSelected: (options) {
_selectedOptions =
options.map((item) => item.value!).toList();
},
options: repeatDaysChoices
.map((item) => ValueItem(label: item, value: item))
.toList(),
maxItems: 5,
selectionType: SelectionType.multi,
chipConfig: ChipConfig(
wrapType: WrapType.wrap,
backgroundColor: Theme.of(context).colorScheme.primary),
optionTextStyle: const TextStyle(fontSize: 14),
selectedOptionIcon: Icon(
Icons.check_circle,
color: Theme.of(context).colorScheme.primary,
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
InkWell(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const RepeatTaskScreen(),
)),
child: Container(
width: 400,
margin: const EdgeInsets.all(15),
child: Row(
children: [
Text(
repeatDays,
style: TextStyle(
color: theme.isLightMode
? Colors.black
: Colors.white,
),
),
const Spacer(),
Icon(
(Icons.add_call),
color:
theme.isLightMode ? Colors.black : Colors.white,
)
],
),
),
),
selectedOptionTextColor:
Theme.of(context).colorScheme.primary,
dropdownMargin: 1,
onOptionRemoved: (index, option) {},
),
),
const Spacer(),
],
)
],
),
floatingActionButton: !textformKeyIsEmpty
Expand All @@ -298,7 +290,7 @@ class _TasksScreenState extends ConsumerState<TasksScreen> {
taskName: taskController.text,
date: enteredDate!,
time: selectedTime!,
repeatTaskDays: repeatDays!));
repeatTaskDays: repeatDays));
return;
}
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
Expand Down
37 changes: 37 additions & 0 deletions lib/screens/set_repeat_task_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import 'package:to_do_app/screens/widgets/repeat_task_container.dart';

class RepeatTaskScreen extends StatefulWidget {
const RepeatTaskScreen({super.key});

@override
State<RepeatTaskScreen> createState() => _RepeatTaskScreenState();
}

class _RepeatTaskScreenState extends State<RepeatTaskScreen> {
List<String> repeatDaysChoices = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Repeat"),
),
body: ListView.builder(
itemCount: repeatDaysChoices.length,
itemBuilder: (context, index) {
return TaskContainer(
leadingText: repeatDaysChoices[index],
);
},
),
);
}
}
5 changes: 5 additions & 0 deletions lib/screens/widgets/home_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ class _HomeBodyState extends ConsumerState<HomeBody> {
DateTime currentDate = DateTime.now();
DateTime nextWeek = currentDate.add(const Duration(days: 7));
DateTime nextMonth = currentDate.add(const Duration(days: 30));
DateTime lastWeek = currentDate.subtract(const Duration(days: 7));
DateTime lastMonth = currentDate.subtract(const Duration(days: 30));

return [
if (currentDate.isBefore(nextWeek)) "Due This Week",
if (currentDate.isBefore(nextMonth)) "Due Next Week",
if (nextMonth.month == currentDate.month) "Due Later This Month",
if (nextMonth.month != currentDate.month) "Due Next Month",
if (lastWeek.isBefore(currentDate)) "Due Last Week",
if (lastMonth.month == currentDate.month) "Due Last Month",
if (lastMonth.month != currentDate.month) "Due Earlier This Month",
"Due Later"
];
}
Expand Down
46 changes: 46 additions & 0 deletions lib/screens/widgets/repeat_task_container.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:to_do_app/providers/settings_provider.dart';

class TaskContainer extends ConsumerStatefulWidget {
const TaskContainer({super.key, required this.leadingText});

final String leadingText;

@override
ConsumerState<TaskContainer> createState() => _TaskContainerState();
}

class _TaskContainerState extends ConsumerState<TaskContainer> {
@override
Widget build(BuildContext context) {
String selectedDays;
bool isPressed = false;

final theme = ref.watch(settingsProvider);

return Padding(
padding: const EdgeInsets.only(top: 20, left: 10, right: 10),
child: InkWell(
onTap: () {
setState(() {
isPressed = !isPressed;
});
},
child: ListTile(
leadingAndTrailingTextStyle: const TextStyle(fontSize: 17),
dense: true,
textColor: theme.isLightMode ? Colors.black : Colors.white,
leading: Text(widget.leadingText),
trailing: isPressed == true
? Icon(
Icons.check,
color: Theme.of(context).colorScheme.primary,
size: 30,
grade: BorderSide.strokeAlignOutside,
fill: 20,
)
: null)),
);
}
}
4 changes: 3 additions & 1 deletion lib/services/local Notifications/local_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class LocalNotificationsService {
return await flutterLocalNotificationsPlugin.getActiveNotifications();
}

void scheduleNotifications(ToDo todo) async {
void scheduleNotifications(
ToDo todo,
) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ class LocalNotificationsInitializer {
return _flutterLocalNotificationsPlugin;
}

void initialisePlugin(InitializationSettings initializationSettings) async {
void initialisePlugin() async {
var initializationSettings = await LocalNotificationsInitializer()
.initializeNotificationCategories();
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse:
(NotificationResponse notificationResponse) async {
// ...
}, onDidReceiveBackgroundNotificationResponse: notificationTapBackground);
}

Future<void> initializeNotificationCategories() async {
Future<InitializationSettings> initializeNotificationCategories() async {
final DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
// ... requestSoundPermission: false,
Expand Down Expand Up @@ -59,12 +61,6 @@ class LocalNotificationsInitializer {
// Perform the initialization steps
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');

final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
);
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'...',
Expand All @@ -76,11 +72,15 @@ class LocalNotificationsInitializer {
);
const NotificationDetails(android: androidNotificationDetails);

initialisePlugin(initializationSettings);
return InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
);
}

@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
static void notificationTapBackground(
NotificationResponse notificationResponse) {
// handle action
}
void notificationResponse(NotificationResponse notificationResponse) {
Expand Down

0 comments on commit 5242c05

Please sign in to comment.