如何从 TZDateTime 内的变量设置小时和分钟?:Flutter

问题描述 投票:0回答:1

我安排用户通知。它工作得很好,但我想添加来自用户的小时和分钟,只有我有两个变量,小时和分钟,但我不知道将变量放在哪里。我试图将它们与年、月、日放在一起,但无济于事。我能实现吗?

示例:

    var  schduelTime =  tz.TZDateTime(
          tz.local,
          currentzone.year,
          currentzone.month,
          currentzone.day,
          My_hour,
          My_minute,

        );

//代码

import 'package:adhdhkar_muslim/virb_uli.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'dart:math';
import 'package:adhdhkar_muslim/virb_uli.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'package:flutter_timezone/flutter_timezone.dart';
import 'virb_uli.dart';
class NotificationService {
  static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

   static Future init()async{
    // ignore: unused_local_variable
    InitializationSettings settings = const InitializationSettings(
      android: AndroidInitializationSettings('@mipmap/ic_launcher'),
      iOS: DarwinInitializationSettings(),
    );

    flutterLocalNotificationsPlugin.initialize(
      settings,
      
     // onDidReceiveBackgroundNotificationResponse:(details) {},
      //onDidReceiveNotificationResponse:(details){},
    );

   }

static void showBasicNotification() async {
  const AndroidNotificationDetails android = AndroidNotificationDetails(
    'id 3',
    "schdulrd Notf",
    importance: Importance.max,
    priority: Priority.high,

  );
  NotificationDetails details = const NotificationDetails(
    android: android,
  );
  tz.initializeTimeZones();
 
 var  currentzone = tz.TZDateTime.now(tz.local);
 
 var  schduelTime =  tz.TZDateTime(
          tz.local,
          currentzone.year,
          currentzone.month,
          currentzone.day,
          19,
          30,

        );
if (schduelTime.isBefore(currentzone)) {

 schduelTime = schduelTime.add(const Duration(days: 1));
   //schduelTime = schduelTime.add(const Duration(hours: 1));
   
 }
await flutterLocalNotificationsPlugin.zonedSchedule(
   1,
  "اذكار الصباح ",
  "الله اكبر",
  schduelTime
   ,
  details,
   uiLocalNotificationDateInterpretation:UILocalNotificationDateInterpretation.absoluteTime);

}

}

我不想像代码中那样手动添加时间。我不想这样

flutter notifications timezone android-notifications flutter-local-notification
1个回答
0
投票

也许您可以使用“添加”方法,如下所示

schduelTime = schduelTime.add(const Duration(days: 1));

但是您可以用一个简单的方程式替换持续时间,该方程式减去用户从 DateTime.now() 输入的日期和时间。

现在时间是早上 05:30,想要下午 02:30,所以减去它,结果将是 3 小时。 所以你用“add”方法添加了这 3 个小时。

© www.soinside.com 2019 - 2024. All rights reserved.