Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

dart - flutter alarm clock not getting sound

I'm working currently on a flutter project

the app is working fine but i have some issues:

1-i get the notification but i don't get any sound from the notification, i don't know if this is flutter_local_notifications dependency problem because when i tried to update it to the latest version i got few errors from main page and alarm page

2-i can't access these options(Repeat,Sound,Title):

https://i.ibb.co/4fj7x16/s3.png

here is the alarm page:

      import 'package:clock_app/alarm_helper.dart';
      import 'package:clock_app/constants/theme_data.dart';
      import 'package:clock_app/data.dart';
      import 'package:clock_app/models/alarm_info.dart';
      import 'package:dotted_border/dotted_border.dart';
      import 'package:flutter/material.dart';
      import 'package:flutter_local_notifications/flutter_local_notifications.dart';
      import 'package:intl/intl.dart';

      import '../main.dart';

  class AlarmPage extends StatefulWidget {
  @override
 _AlarmPageState createState() => _AlarmPageState();
 }

 class _AlarmPageState extends State<AlarmPage> {
 DateTime _alarmTime;
 String _alarmTimeString;
 AlarmHelper _alarmHelper = AlarmHelper();
 Future<List<AlarmInfo>> _alarms;

 @override
 void initState() {
_alarmTime = DateTime.now();
_alarmHelper.initializeDatabase().then((value) {
  print('------database intialized');
  loadAlarms();
 });
 super.initState();
 }

void loadAlarms() {
_alarms = _alarmHelper.getAlarms();
if (mounted) setState(() {});
}

 @override
 Widget build(BuildContext context) {
 return Container(
  padding: EdgeInsets.symmetric(horizontal: 32, vertical: 64),
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Text(
        'Alarm',
        style: TextStyle(
            fontFamily: 'avenir',
            fontWeight: FontWeight.w700,
            color: CustomColors.primaryTextColor,
            fontSize: 24),
      ),
      Expanded(
        child: FutureBuilder<List<AlarmInfo>>(
          future: _alarms,
          builder: (context, snapshot) {
            if (snapshot.hasData)
              return ListView(
                children: snapshot.data.map<Widget>((alarm) {
                  var alarmTime =
                      DateFormat('hh:mm aa').format(alarm.alarmDateTime);
                  var gradientColor = GradientTemplate
                      .gradientTemplate[alarm.gradientColorIndex].colors;
                  return Container(
                    margin: const EdgeInsets.only(bottom: 32),
                    padding: const EdgeInsets.symmetric(
                        horizontal: 16, vertical: 8),
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        colors: gradientColor,
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                      ),
                      boxShadow: [
                        BoxShadow(
                          color: gradientColor.last.withOpacity(0.4),
                          blurRadius: 8,
                          spreadRadius: 2,
                          offset: Offset(4, 4),
                        ),
                      ],
                      borderRadius: BorderRadius.all(Radius.circular(24)),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Row(
                              children: <Widget>[
                                Icon(
                                  Icons.label,
                                  color: Colors.white,
                                  size: 24,
                                ),
                                SizedBox(width: 8),
                                Text(
                                  alarm.title,
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontFamily: 'avenir'),
                                ),
                              ],
                            ),
                            Switch(
                              onChanged: (bool value) {},
                              value: true,
                              activeColor: Colors.white,
                            ),
                          ],
                        ),
                        Text(
                          'Mon-Fri',
                          style: TextStyle(
                              color: Colors.white, fontFamily: 'avenir'),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              alarmTime,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontFamily: 'avenir',
                                  fontSize: 24,
                                  fontWeight: FontWeight.w700),
                            ),
                            IconButton(
                              icon: Icon(Icons.delete),
                              color: Colors.white,
                              onPressed: () {
                                _alarmHelper.delete(alarm.id);
                              },
                            ),
                          ],
                        ),
                      ],
                    ),
                  );
                }).followedBy([
                  if (alarms.length < 5)
                    DottedBorder(
                      strokeWidth: 2,
                      color: CustomColors.clockOutline,
                      borderType: BorderType.RRect,
                      radius: Radius.circular(24),
                      dashPattern: [5, 4],
                      child: Container(
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: CustomColors.clockBG,
                          borderRadius:
                              BorderRadius.all(Radius.circular(24)),
                        ),
                        child: FlatButton(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 32, vertical: 16),
                          onPressed: () {
                            _alarmTimeString =
                                DateFormat('HH:mm').format(DateTime.now());
                            showModalBottomSheet(
                              useRootNavigator: true,
                              context: context,
                              clipBehavior: Clip.antiAlias,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.vertical(
                                  top: Radius.circular(24),
                                ),
                              ),
                              builder: (context) {
                                return StatefulBuilder(
                                  builder: (context, setModalState) {
                                    return Container(
                                      padding: const EdgeInsets.all(32),
                                      child: Column(
                                        children: [
                                          FlatButton(
                                            onPressed: () async {
                                              var selectedTime =
                                                  await showTimePicker(
                                                context: context,
                                                initialTime:
                                                    TimeOfDay.now(),
                                              );
                                              if (selectedTime != null) {
                                                final now = DateTime.now();
                                                var selectedDateTime =
                                                    DateTime(
                                                        now.year,
                                                        now.month,
                                                        now.day,
                                                        selectedTime.hour,
                                                        selectedTime
                                                            .minute);
                                                _alarmTime =
                                                    selectedDateTime;
                                                setModalState(() {
                                                  _alarmTimeString =
                                                      selectedTime
                                                          .toString();
                                                });
                                              }
                                            },
                                            child: Text(
                                              _alarmTimeString,
                                              style:
                                                  TextStyle(fontSize: 32),
                                            ),
                                          ),
                                          ListTile(
                                            title: Text('Repeat'),
                                            trailing: Icon(
                                                Icons.arrow_forward_ios),
                                          ),
                                          ListTile(
                                            title: Text('Sound'),
                                            trailing: Icon(
                                                Icons.arrow_forward_ios),
                                          ),
                                          ListTile(
                                            title: Text('Title'),
                                            trailing: Icon(
                                                Icons.arrow_forward_ios),
                                          ),
                                          FloatingActionButton.extended(
                                            onPressed: () async {
                                              DateTime
                                                  scheduleAlarmDateTime;
                                              if (_alarmTime
                                                  .isAfter(DateTime.now()))
                                                scheduleAlarmDateTime =
                                                    _alarmTime;
                                              else
                                                scheduleAlarmDateTime =
                                                    _alarmTime.add(
                                                        Duration(days: 1));

                                              var alarmInfo = A

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

in flutter local notification, you can set the alarm to be insisted by setting extra flag 4, and thus keep the alarm repeat until user tap on it

for more detail click here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...