如何在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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...