How To Get Current Date With Day In Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

we are using the DateTime class to extract only current Time from mobile. The DateTime class works perfectly in both Android and iOS platform.

How To Get Current Date With Day In Flutter Android App

Complete Code For  Get Current Date With Day In Flutter
main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(
              centerTitle: true,
                backgroundColor: Colors.pink,
                title: Text('Get Current Date')
            ),
            body: Center(
                child: CurrentDate()
            )
        )
    );
  }
}

class CurrentDate extends StatefulWidget {

  _CurrentDateState createState() => _CurrentDateState();

}

class _CurrentDateState extends State<CurrentDate> {
  String finalDate = '';
  getCurrentDate(){
    var date = new DateTime.now().toString();
    var dateParse = DateTime.parse(date);
    var formattedDate = "${dateParse.day}-${dateParse.month}-${dateParse.year}";
    setState(() {
      finalDate = formattedDate.toString() ;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              Padding(
                  padding: EdgeInsets.all(10.0),
                  child :
                  Text("Date : $finalDate", style: TextStyle(
                      fontSize: 18), textAlign: TextAlign.center,
                  )
              ),
              RaisedButton(
                onPressed: getCurrentDate,
                color: Colors.pink,
                textColor: Colors.white,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                child: Text('Current Date'),
              ),
            ],
          ),
        ));
  }
}

Related Post