get_time_ago 0.0.6 get_time_ago: ^0.0.6 copied to clipboard
A new Flutter package to format DateTime in seconds ago, minutes ago, hours ago, days ago and date.
import 'package:flutter/material.dart';
import 'package:get_time_ago/get_time_ago.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "GetTimeAgo Example",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.deepPurple,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
);
}
}
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),
)
],
),
),
),
);
}
}