Flutter Create Alarm
Step 1: Adding dependency
Run this command:
flutter pub add flutter_alarm_clock
or Add
dependencies:
flutter_alarm_clock: ^0.0.2(Version can be vary ! use latest one )
Step 2: Now, import the library in the file where you want to work with the clock.
import 'package:flutter_alarm_clock/flutter_alarm_clock.dart';
To Set alarm :
You need to pass two parameters one is hour and another one is minute .
the method looks like this:
hour = int.parse(hourController.text.trim());
minutes = int.parse(minuteController.text.trim());
FlutterAlarmClock.createAlarm(hour, minutes);
Full Source Code For Alarm Page
class AlarmPage extends StatefulWidget {
@override
State<AlarmPage> createState() => _AlarmPageState();
}
class _AlarmPageState extends State<AlarmPage> {
TextEditingController hourController = TextEditingController();
TextEditingController minuteController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.cyan,
title: const Text('Alarm'),
centerTitle: true,
),
body:
Center(
child: Column(
children: [
SizedBox(height: 130),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
height: 50,
width: 160,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.cyan,
),
child: Center(
child: TextField(
textAlign: TextAlign.center,
controller: hourController,
keyboardType: TextInputType.number,
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Hour',
hintStyle: TextStyle(
color: Colors.white70,
fontSize: 24,
),
),
),
),
),
SizedBox(width: 20),
Container(
height: 50,
width: 160,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.cyan,
),
child: Center(
child: TextField(
textAlign: TextAlign.center,
controller: minuteController,
keyboardType: TextInputType.number,
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Minute',
hintStyle: TextStyle(
color: Colors.white70,
fontSize: 24,
),
),
),
),
),
],
),
SizedBox(height: 50,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [ Container(
margin: const EdgeInsets.all(25),
child: ElevatedButton(
onPressed: () {
int hour;
int minutes;
hour = int.parse(hourController.text);
minutes = int.parse(minuteController.text);
FlutterAlarmClock.createAlarm(hour, minutes);
},
child: const Text(
'Create Alarm',
style: TextStyle(fontSize: 20.0),
),
style: ElevatedButton.styleFrom(
primary: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(11),
),
),
),
),
ElevatedButton(
onPressed: () {
FlutterAlarmClock.showAlarms();
},
child: const Text(
'Show Alarms',
style: TextStyle(fontSize: 20.0),
),
style: ElevatedButton.styleFrom(
primary: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(11),
),
),
),],),
],
),
),
);
}
}
Output Screenshot