मैं दो दिनांक समय विजेट के डेटा घटाना चाहता हूँ। क्या आप कृपया मुझे इसका समाधान सुझा सकते हैं। नीचे मैंने अपने आउटपुट का उल्लेख किया है कि मैं इस कोड के माध्यम से क्या चाहता हूं

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 में घटाई गई तारीख का परिणाम देना चाहिए। आशा है आप मेरी समस्या को समझ गए होंगे।

0
Shîvam Yadav 25 अगस्त 2020, 17:58

1 उत्तर

सबसे बढ़िया उत्तर

आप नीचे दिए गए कोड को आजमा सकते हैं:

int parsedTimeResult = 0;

Function TimeSubtraction() {
    setState(() {
      parsedTimeResult = selectedDate2.difference(selectedDate1).inDays;
    });
  }
0
Lapa Ny Aina Tanjona 25 अगस्त 2020, 19:12