How can I get AM or Pm at the end using TimeofDay and how can I add some text on that if it is null?

166 views Asked by At

I want to add AM or PM at the end of time and for that I have tried doing like DateFormat('hh:mm a') it add AM or PM but then it picks a wrong time. How to fix this? Pls help me:-

Future selectTimeEnd() async {
final TimeOfDay picked = await showTimePicker(
  context: context,
  initialTime: selectedTime,
  builder: (BuildContext context, Widget child){
    return Theme(
      data: ThemeData.dark(),
      child: child,
    );
  });
if (picked != null)
  setState(() {
    selectedTime = picked;
    _hour = selectedTime.hour.toString();
    _minute = selectedTime.minute.toString();
    _time = _hour + ' : ' + _minute;
    _timeControllerEnd.text = _time;  ///this _time displays the selected time.

    var inputDate = DateTime(selectedTime.hour,selectedTime.minute);
    var outputFormat = DateFormat('hh:mm a'); ///but this shows wrong selected time 
    var outputDate = outputFormat.format(inputDate);
    print(outputDate);

  });

}

And if the selected time is null then the text filed remains empty but I want some text hint on that place and for that I have tried this:-

 String _dropTime = _timeControllerEnd.text != null ? _timeControllerEnd.text : "Until";
    child: Align(
         alignment: Alignment.centerLeft,
         child: Text(_dropTime != null ? _dropTime : "Until",textAlign: TextAlign.start,
         style: TextStyle(fontSize: 20)),
      ),
1

There are 1 answers

2
Muhammad Arbaz Zafar On

change this code

child: Text(_dropTime != null ? _dropTime : "Until",textAlign: TextAlign.start,
         style: TextStyle(fontSize: 20)),

to this

chid: _dropTime!=null?Text(_dropTime):Text("untill"),