如何解决如何在flutter中添加或减去两个日期选择器?
我想减去两个日期时间小部件的数据。您能为我建议解决方案吗?下面我提到了我的输出,我想要通过这段代码
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,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),onPressed: () {
TimeSubtraction();
},color: Colors.redAccent,child: Text(
'Result',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,);
}
}
当我单击按钮时,它应该以DAYS,SECONDS和HOURS为单位给出减去日期的结果。 希望你了解我的问题。
解决方法
您可以尝试以下代码:
_encoded_results
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。