Displaying time selected from TimePicker to display Local Notification in Flutter App

1

I want to show Local Notification at the time that I have selected using TimePicker. I can call the function of scheduleAlaram() but I cannot send the time that i selected. Please help me out with how do I send this time to my function in order to display local notification.

This is my code:

import 'package:epicare/Medicines.dart';
import 'package:flutter/material.dart';
import 'package:flutter_switch/flutter_switch.dart';

class AddMedicine extends StatefulWidget {
  @override
  _AddMedicineState createState() => _AddMedicineState();
}

class _AddMedicineState extends State<AddMedicine> {
  final timeController = TextEditingController();

 Widget build(BuildContext context) {
    String value = "";
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(),

body: SingleChildScrollView(
        child: Column(
          children: [
Container(
              width: size.width,
              padding: EdgeInsets.symmetric(horizontal: 34, vertical: 13),
              child: Text(
                'Reminder',
                style: TextStyle(
                  fontFamily: 'Montserrat',
                  fontSize: 14,
                  color: const Color(0xff000000),
                  fontWeight: FontWeight.w600,
                ),
                textAlign: TextAlign.left,
              ),
            ),
            Container(
              width: size.width * 0.85,
              height: 52,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6.0),
                color: const Color(0xffffffff),
                border: Border.all(width: 1.0, color: const Color(0xffdbe2ea)),
                boxShadow: [
                  BoxShadow(
                    color: const Color(0x0a2c2738),
                    offset: Offset(0, 4),
                    blurRadius: 8,
                  ),
                ],
              ),
              child: TextField(
                controller: timeController,
                readOnly: true,
                style: TextStyle(
                  fontFamily: 'Montserrat',
                  fontSize: 12,
                  color: const Color(0xff000000),
                  fontWeight: FontWeight.w500,
                ),
                decoration: InputDecoration(
                  suffixIcon: IconButton(
                    onPressed: () async {
                      var time = await showTimePicker(
                        builder: (BuildContext context, Widget child) {
                          return Theme(
                            data: ThemeData.light().copyWith(
                              colorScheme: ColorScheme.dark(
                                primary: const Color(0xffE5E0A1),
                                onPrimary: Colors.black,
                                surface: Colors.white,
                                onSurface: Colors.black,
                              ),
                              dialogBackgroundColor:Colors.white,
                            ),
                            child: child,
                          );
                        },
                          context: context,
                          initialTime: TimeOfDay.now(),
                          
                      );
                      print(time.format(context));
                      timeController.text = time.format(context);
                      scheduleAlarm();

                    },
                    icon: Icon(
                      Icons.add_alert,
                      color: const Color(0xffd4d411),
                    ),
                  ),
                  contentPadding:
                  const EdgeInsets.symmetric(vertical: 18, horizontal: 20),
                  border: InputBorder.none,
                  hintText: "Add time(s) to eat your medicine",
                  hintStyle: TextStyle(
                    fontFamily: 'Montserrat',
                    fontSize: 13,
                    color: const Color(0xffcbd0d6),
                  ),
                ),
                onChanged: (text) {
                  value = text;
                  print(value);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
void scheduleAlarm() async {
    //print("hello");
    var time = Time(2, 37, 0);
    var androidPlatformChannelSpecifics =
    AndroidNotificationDetails('repeatDailyAtTime channel id',
        'repeatDailyAtTime channel name', 'repeatDailyAtTime description');
    var iOSPlatformChannelSpecifics =
    IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.showDailyAtTime(
        0,
        'show daily title',
        'Daily notification shown at approximately ${(time.hour)}:${(time.minute)}}',
        time,
        platformChannelSpecifics);
  }
}

This is the screen which I am currently working on and I am on Reminder section to display notification:

enter image description here

flutter
dart
asked on Stack Overflow Apr 22, 2021 by Nabia Salman • edited Apr 24, 2021 by Nabia Salman

1 Answer

1

how do I send this time to my function in order to display local notification.

Update showAlarm() to accept an argument for the time like so:

void scheduleAlarm(Time time) {
...
}

Since showDailyAtTime() accepts a value of Time, you should call the updated function after converting the value from the textcontroller to a Time value,

Time.format(context) will return a String so it cannot be used,

However you can modify this line var time = Time(2, 37, 0); to create a time value from the inputvalue and convert it to time using Time() which can then be sent to the scheduleAlarm() function

answered on Stack Overflow Apr 24, 2021 by Rohan Thacker • edited Apr 24, 2021 by Rohan Thacker

User contributions licensed under CC BY-SA 3.0