get_time_ago 2.1.0 copy "get_time_ago: ^2.1.0" to clipboard
get_time_ago: ^2.1.0 copied to clipboard

A Dart package to convert and format `DateTime` objects into human-readable 'time ago' strings like '20 seconds ago', 'a minute ago', or '7 hours ago'.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:get_time_ago/get_time_ago.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(GetTimeAgoExampleApp());
}

class GetTimeAgoExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'GetTimeAgo Example',
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
        brightness: Brightness.light,
        primarySwatch: Colors.blue,
      ),
      darkTheme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
        brightness: Brightness.dark,
        primarySwatch: Colors.blue,
      ),
      home: GetTimeAgoExampleScreen(),
    );
  }
}

class GetTimeAgoExampleScreen extends StatefulWidget {
  @override
  State<GetTimeAgoExampleScreen> createState() =>
      _GetTimeAgoExampleScreenState();
}

const Map<String, String> languageNames = {
  'ar': 'Arabic',
  'en': 'English',
  'es': 'Spanish',
  'fa': 'Persian (Farsi)',
  'fr': 'French',
  'hi': 'Hindi',
  'pt': 'Portuguese (Brazil)',
  'br': 'Portuguese (Brazil alternate)',
  'zh': 'Simplified Chinese',
  'zh_tr': 'Traditional Chinese',
  'ja': 'Japanese',
  'oc': 'Occitan',
  'ko': 'Korean',
  'de': 'German',
  'id': 'Indonesian',
  'tr': 'Turkish',
  'ur': 'Urdu',
  'vi': 'Vietnamese',
  'ro': 'Romanian',
  'nl': 'Dutch',
};

class _GetTimeAgoExampleScreenState extends State<GetTimeAgoExampleScreen> {
  // Helper function to get the DateTime relative to now
  DateTime _getRelativeDateTime(Duration duration) {
    return DateTime.now().subtract(duration);
  }

  // Variable to hold the selected language code
  String? _selectedLanguage = Data.defaultLocale;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GetTimeAgo Example'),
        centerTitle: true,
        backgroundColor: Theme.of(context).appBarTheme.backgroundColor,
      ),
      body: SafeArea(
        child: Center(
          child: Container(
            padding: const EdgeInsets.all(16),
            margin: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              borderRadius: const BorderRadius.all(Radius.circular(16.0)),
              border: Border.all(color: Colors.grey.shade300),
            ),
            child: Column(
              children: [
                DropdownButton<String>(
                  borderRadius: const BorderRadius.all(Radius.circular(16.0)),
                  isExpanded: true,
                  hint: const Text("Select Locale"),
                  value: _selectedLanguage,
                  onChanged: (String? newValue) {
                    setState(() {
                      _selectedLanguage = newValue;
                    });
                  },
                  items: languageNames.entries.map((entry) {
                    var code = entry.key;
                    var name = entry.value;
                    return DropdownMenuItem<String>(
                      value: code,
                      child: Text('$name ($code)'),
                    );
                  }).toList(),
                ),
                Expanded(
                  child: ListView(
                    shrinkWrap: true,
                    physics: const BouncingScrollPhysics(),
                    children: [
                      _buildExampleTile(
                        context,
                        'Just Now (Less than 15 seconds)',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(seconds: 10)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '30 seconds ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(seconds: 30)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '1 minute ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(minutes: 1)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '2 minutes ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(minutes: 2)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '1 hour ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(hours: 1)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '2 hours ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(hours: 2)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '1 day ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(days: 1)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '3 days ago',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(days: 3)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        '10 days ago (beyond 7 days)',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(const Duration(days: 10)),
                          locale: _selectedLanguage,
                        ),
                      ),
                      _buildExampleTile(
                        context,
                        'Custom format beyond 7 days',
                        GetTimeAgo.parse(
                          _getRelativeDateTime(
                            const Duration(days: 10),
                          ),
                          pattern: 'yyyy-MM-dd',
                          locale: _selectedLanguage,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildExampleTile(BuildContext context, String title, String timeAgo) {
    return ListTile(
      title: Text(title),
      subtitle: Text(
        timeAgo,
        style: const TextStyle(
          color: Colors.blue,
          fontSize: 16,
        ),
      ),
      isThreeLine: true,
    );
  }
}
138
likes
150
points
7.66k
downloads

Publisher

verified publishernixrajput.com

Weekly Downloads

A Dart package to convert and format `DateTime` objects into human-readable 'time ago' strings like '20 seconds ago', 'a minute ago', or '7 hours ago'.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#get-time-ago #gettimeago #timeago #time-ago

Documentation

API reference

Funding

Consider supporting this project:

ko-fi.com
buymeacoffee.com
github.com

License

MIT (license)

Dependencies

intl

More

Packages that depend on get_time_ago