get_time_ago 0.0.5 get_time_ago: ^0.0.5 copied to clipboard
A new Flutter package for getting time formats in secons, minutes, hours, days and date.
import 'package:flutter/material.dart';
import 'package:get_time_ago/get_time_ago.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var _dateTime = DateTime.now();
var _timestamp = '2020-09-10 05:21:37.712498';
Future<void> _selectDate() async {
DateTime pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime.now());
if (pickedDate != null) {
setState(() {
_dateTime = pickedDate;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GetTimeAgo Example"),
),
body: Container(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton.icon(
onPressed: _selectDate,
label: Text("Select DateTime"),
color: Theme.of(context).accentColor,
textColor: Colors.white,
icon: Icon(Icons.calendar_today),
),
SizedBox(height: 20.0),
Text(
"Converting DateTime to TimeAgo",
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
),
SizedBox(height: 10.0),
Text(_dateTime.toString()),
Text(
TimeAgo.getTimeAgo(_dateTime),
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold),
),
SizedBox(height: 20.0),
Text(
"Converting String to TimeAgo",
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
),
SizedBox(height: 10.0),
Text(_timestamp),
Text(
TimeAgo.getTimeAgo(DateTime.parse(_timestamp)),
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold),
)
],
),
),
),
);
}
}