skip to Main Content

I have an app that I want to disable the button after 8 hours of use per day, being able to use it as many times as I want within 8 hours of the day.

Below is part of the button code:

children: [
                if (onMenuButtonPressed != null)
                  Align(
                    alignment: Alignment.centerLeft,
                    child: CupertinoButton(
                      onPressed: onMenuButtonPressed,
                      //onPressed: (DateTime.now().second >= 1 && DateTime.now().second <= 12) ? null : () => onMenuButtonPressed,
                      minSize: 0,
                      padding: const EdgeInsets.all(8),
                      child: const Icon(
                        Ionicons.menu,
                        color: ColorPalette.neutral50,
                      ),
                    ),
                  ),

I tried to include the code below, but it didn’t work and it was commented out:

onPressed: (DateTime.now().second >= 1 && DateTime.now().second <= 12) ? null : () => onMenuButtonPressed,

2

Answers


  1. Try your code this way,

    DateTime? buttonPressedTime;
    
    void handleMenuButtonPressed() {
      if (buttonPressedTime == null || buttonPressedTime!.day != DateTime.now().day) {
        buttonPressedTime = DateTime.now();
      }
    
      if (DateTime.now().difference(buttonPressedTime!) >= Duration(hours: 8)) {
        // Disable the button
        return;
      }
    
    
    }
    
    // Usage of the button
    children: [
      if (onMenuButtonPressed != null)
        Align(
          alignment: Alignment.centerLeft,
          child: CupertinoButton(
            onPressed: handleMenuButtonPressed,
            minSize: 0,
            padding: const EdgeInsets.all(8),
            child: const Icon(
              Ionicons.menu,
              color: ColorPalette.neutral50,
            ),
          ),
        ),
    ],
    
    Login or Signup to reply.
  2. Try your code this way,

    DateTime? buttonPressedTime;
    
    
    void handleMenuButtonPressed() {
      if (buttonPressedTime == null || buttonPressedTime!.day != DateTime.now().day) {
        buttonPressedTime = DateTime.now();
      }
    
      if (DateTime.now().difference(buttonPressedTime!) >= Duration(hours: 8)) {
        // Reset the buttonPressedTime to allow usage on the next day
        buttonPressedTime = DateTime.now();
        // Disable the button
        return;
      }
    
      
    }
    
    // Usage of the button
    children: [
      if (onMenuButtonPressed != null)
        Align(
          alignment: Alignment.centerLeft,
          child: CupertinoButton(
            onPressed: handleMenuButtonPressed,
            minSize: 0,
            padding: const EdgeInsets.all(8),
            child: const Icon(
              Ionicons.menu,
              color: ColorPalette.neutral50,
            ),
          ),
        ),
    ],
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search