Sander Seiersnes
Sander Seiersnes

Reputation: 27

Check if it is specific day in Dart

I want to make a app that checks if it is Friday, if it is Friday I want it to say Yes and no if it is not Friday, now it says no automatic and when I press on the FloatingActionButton it says Yes. How can I make it check and then send No or Yes to the app. Current code:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: App(),
    ));

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

String isItFriday = 'No';

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            isItFriday = 'Yes';
          });
        },
        child: Icon(Icons.add),
        
      ),
      body: Padding(
        padding: EdgeInsets.fromLTRB(30, 250, 30, 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Center(
              child: Text(
                'Is it friday?',
              ),
            ),
            Center(
              child: Text(
                '$isItFriday',
              ),
            ),
          ],
        ),
      ),
    );
  }
}

                

Upvotes: 0

Views: 765

Answers (2)

Robert Sandberg
Robert Sandberg

Reputation: 8607

In your setState method that is called in your floating action button just change this line:

isItFriday = 'Yes';

to this line:

isItFriday = DateTime.now().weekday == DateTime.friday ? 'Yes' : 'No';

Upvotes: 2

Ibrahim Ali
Ibrahim Ali

Reputation: 2503

Import this

import 'package:intl/intl.dart';

Then now you can use the format date

if( DateFormat('EEEE').format(DateTime.now() == 'Friday') print('Its Friday')

Or You can do it like this

 ( DateTime.now().weekday == 5 ) print('Its Friday')

Upvotes: 0

Related Questions