英文:
the calculation of the month
问题
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++;
}
});
}
我想使用这个方法来更改月份。但如果我在202301运行'mius',我希望它变成202212而不是202300。有人可以帮助我吗?
英文:
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?
答案1
得分: 0
Here is the translated code:
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')),
),
],
)
],
),
);
}
}
Is there anything else you need?
英文:
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'))),
],
)
],
),
);
}
}
答案2
得分: 0
我猜你让你的代码变得复杂了:
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++;
}
});
}
通常,月份的计算与普通的整数计算不同,所以不要使用整数进行计算,而是使用DateTime对象进行计算。
示例:
DateTime defaultmonth = DateTime.now();
var finalDefaultmonth;
@override
void minus() {
setState(() {
if (defaultmonth.year != DateTime.now().year - 1 || defaultmonth.month != 1) {
DateTime newDateTime = DateTime(defaultmonth.year, defaultmonth.month - 1);
finalDefaultmonth = DateFormat('yyyyMM').format(newDateTime);
}
});
}
英文:
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);
}
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论