android - 如何在颤动中添加或减去两个日期选择器?
问题描述
我想减去两个日期时间小部件的数据。你能建议我解决这个问题吗?下面我通过这段代码提到了我想要的输出
import 'package:AllInOneCalci/customAppBar.dart';
import 'package:flutter/material.dart';
class AgeCalcUI extends StatelessWidget {
@override
Widget build(BuildContext context) {
var AppBarHeight = MediaQuery.of(context).size.height;
return Scaffold(
appBar: customAppBar(
height: (AppBarHeight / 3) * 0.4,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 18.0),
child: Text(
'All In One Cali',
style: TextStyle(
color: Colors.black,
fontSize: 35.0,
fontFamily: 'DancingScript',
fontWeight: FontWeight.bold),
),
),
],
),
),
body: CustomDatePicker(),
);
}
}
enum DateSelection { first, second }
class CustomDatePicker extends StatefulWidget {
@override
_CustomDatePickerState createState() => _CustomDatePickerState();
}
class _CustomDatePickerState extends State<CustomDatePicker> {
//getting the current date
DateTime selectedDate1 = DateTime.now();
DateTime selectedDate2 = DateTime.now();
DateTime selectedDate = DateTime.now();
Duration DifferenceTime;
int parsedTimeResult;
Future<void> selectDateTime1(
BuildContext context, DateSelection dateSelection) async {
final DateTime chosen = await showDatePicker(
context: context,
initialDate: selectedDate1,
firstDate: DateTime(1700),
lastDate: DateTime(2030),
initialDatePickerMode: DatePickerMode.year,
);
if (chosen != null && chosen != selectedDate2) {
setState(() {
switch (dateSelection) {
case DateSelection.first:
selectedDate1 = chosen;
break;
case DateSelection.second:
selectedDate2 = chosen;
break;
}
});
}
}
Function TimeSubtraction() {
setState(() {
DifferenceTime = (selectedDate2.difference(selectedDate1));
String parsedString = DifferenceTime.toString();
parsedTimeResult = int.parse(parsedString);
});
}
//widget for displaying and choosing date onTap
Widget CustomButtonDatePicker(
Color ButtonColor, Color TextColor, DateSelection dateSelection) {
return Column(
children: [
Text(
dateSelection == DateSelection.first
? "${selectedDate1.toLocal()}".split(' ')[0]
: "${selectedDate2.toLocal()}".split(' ')[0],
style: TextStyle(
color: TextColor,
fontSize: 30.0,
fontFamily: 'DancingScript',
fontWeight: FontWeight.bold,
),
),
Padding(
padding: const EdgeInsets.only(top: 18.0),
child: MaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
elevation: 10.0,
onPressed: () {
selectDateTime1(context, dateSelection);
},
color: ButtonColor,
child: Text(
'Select',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
),
),
],
);
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
height: (MediaQuery.of(context).size.height) / 2,
alignment: Alignment.center,
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [Colors.black, Colors.cyan],
begin: Alignment.center,
end: Alignment.topLeft),
borderRadius: new BorderRadius.circular(30.0),
),
child: Column(
children: [
Spacer(
flex: 2,
),
CustomButtonDatePicker(
Colors.redAccent, Colors.cyan[200], DateSelection.first),
Spacer(),
CustomButtonDatePicker(
Colors.cyan[200], Colors.redAccent, DateSelection.second),
Spacer(),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 18.0),
child: MaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
elevation: 10.0,
onPressed: () {
TimeSubtraction();
},
color: Colors.redAccent,
child: Text(
'Result',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
),
),
Container(
alignment: Alignment.center,
width: 250.0,
height: 110.0,
decoration: new BoxDecoration(
color: Colors.black,
borderRadius: new BorderRadius.only(
bottomLeft: Radius.circular(30.0),
bottomRight: Radius.circular(30.0))),
padding: const EdgeInsets.all(15.0),
child: Text(
"$parsedTimeResult days",
style: TextStyle(
color: Colors.cyanAccent,
fontSize: 30.0,
fontFamily: 'DancingScript',
),
),
),
],
);
}
}
当我单击按钮时,它应该给我减去日期的结果,以 DAYS、SECONDS 和 HOURS 为单位。 希望你明白我的问题。
解决方案
你可以试试下面的代码:
int parsedTimeResult = 0;
Function TimeSubtraction() {
setState(() {
parsedTimeResult = selectedDate2.difference(selectedDate1).inDays;
});
}
推荐阅读
- typescript - Ionic 4:Ionic Lab 黑屏错误的解决方法
- python - Kivy 未正确显示(孟加拉语)Unicode 字符
- javascript - 无法解析 json 数据 jquery
- ios - 是否可以在 iOS 中实现对操作系统权限提示的分析?
- c# - .Net Web API 以毫秒为单位绑定日期时间
- google-cloud-platform - 使用不同的传出 IP 执行多个 Google Cloud Functions 的正确方法是什么?
- flutter - 从 IBM Watson 下载 CoreML 文件时出错
- excel - 使用列表中的名称创建新工作表
- rabbitmq - 使用 request-reply 和 pub-sub 进行微服务通信
- python - 针对工作日字符串使用 Pandas .corr