skip to Main Content
var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));

@override
void minus() {
  setState(() {
      if (defaultmonth != DateTime.now().year - 1) {
        defaultmonth--;
      }
  });
}

@override
void add() {
  setState(() {
      if (defaultmonth != DateTime.now().year + 1) {
        defaultmonth++;
      }
  });
}

I want to change the moon using this method. But if I run ‘mius’ on 202301, I want it to be 202212 instead of 202300. Is there anyone who can help me?

2

Answers


  1. import 'package:flutter/Material.dart';
    import 'package:intl/intl.dart';
    
    
    
    class PageTestWidget extends StatefulWidget {
      const PageTestWidget({Key? key}) : super(key: key);
    
      @override
      State<PageTestWidget> createState() => _PageTestWidgetState();
    }
    
    class _PageTestWidgetState extends State<PageTestWidget> {
      var date = DateTime.now();
    
      void minus() {
        setState(() {
          date = date.copyWith(month: date.month - 1);
        });
      }
    
      void add() {
        setState(() {
          date = date.copyWith(month: date.month + 1);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar:  AppBar(title: Text('Demo')),
          body: Column(
            children: [
              const Row(),
              Text(DateFormat('yyyyMM').format(date)),
              const SizedBox(height: 20),
              Row(
                children: [
                  Expanded(
                      child:
                          TextButton(onPressed: minus, child: const Text('minus'))),
                  Expanded(
                      child: TextButton(onPressed: add, child: const Text('add'))),
                ],
              )
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. I guess you made your code complicated:

    var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));
    
    @override
    void minus() {
      setState(() {
          if (defaultmonth != DateTime.now().year - 1) {
            defaultmonth--;
          }
      });
    }
    
    @override
    void add() {
      setState(() {
          if (defaultmonth != DateTime.now().year + 1) {
            defaultmonth++;
          }
      });
    }
    

    Generally the month calculations are different from normal integer calculations so Instead of making calculations from integers you do the calculations on DateTime objects.

    Example:

    DateTime defaultmonth = DateTime.now();
    var finalDefaultmonth;
    @override
    void minus() {
      setState(() {
        if (defaultmonth.year != DateTime.now().year - 1 || currentDateTime.month != 1) {
          DateTime newDateTime = DateTime(currentDateTime.year, currentDateTime.month - 1);
          finalDefaultmonth = DateFormat('yyyyMM').format(newDateTime);
        }
      });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search