如何使用密码验证(dd-mm)格式的当前日期

时间:2019-07-24 06:40:15

标签: flutter dart

我正在尝试构建两个屏幕,其中:

当用户以(dd-mm)格式输入当前日期时,第一个屏幕具有Pincode文本字段,例如:如果今天的日期是24-07,则用户输入2407,则它应该导航到另一个页面,即第二个屏幕

第一个屏幕:Passcode.dart

导入“ package:flutter / material.dart”;

import 'package:flutter_course/HomePage.dart';
//import 'package:pin_entry_text_field/pin_entry_text_field.dart';
import 'package:pin_code_text_field/pin_code_text_field.dart';
import 'package:intl/intl.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: homePage(),
        ));
  }
}

class homePage extends StatefulWidget {
  @override
  _homePageState createState() => _homePageState();
}

class _homePageState extends State<homePage> {
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Stack(children: <Widget>[
      Center(
        child: Image.asset(
          "assets/passcode.jpeg",
          width: size.width,
          height: size.height,
          fit: BoxFit.fill,
        ),
      ),
      Column(
        children: <Widget>[
//      SizedBox(height:200,),
//      SizedBox(width: 300),
          Padding(
            padding: EdgeInsets.only(left: 460, top: 150),
            child: Text("ENTER PASSCODE",
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 38.0,
                    color: Colors.black)),
          ),

          SizedBox(height: 30),
          Padding(
            padding: EdgeInsets.only(left: 460),
            child: PinCodeTextField(
              autofocus: false,

              pinTextStyle: TextStyle(
                  color: Colors.black,
                  fontSize: 40,
                  fontWeight: FontWeight.bold),
              hideCharacter: true,
              maskCharacter: "*",
              // highlight: true,
              // highlightColor: Colors.blue,
              defaultBorderColor: Colors.black,
              hasTextBorderColor: Colors.white,
              hasError: true,
              errorBorderColor: Colors.red,
              //onTextChanged: (String)=>func(context),
              onDone: (String) => func(context),
            ),
          ),
        ],
      )
    ]);
  }
}

void func(context) {
  var now = new DateTime.now();
  var formatter = new DateFormat('MMMMd');
  var formatted = formatter.format(now);
  debugPrint(formatted);
  if (String == formatted) {
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => HomePage()));
  }
}


Second Screen :HomePage.dart 


import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(length: 2,child:Scaffold(
        drawer: Drawer(
          child: Column(
            children: <Widget>[
              AppBar(
                automaticallyImplyLeading: false,
                title: Text('Choose'),
                backgroundColor:Color(0xffedac51),
              ),
              ListTile(
                title: Text('Devices'),
                onTap: () {
                  // Navigator.pushReplacement(
                  // context,
                  // MaterialPageRoute(
                  // builder: (BuildContext context) =>
                  // ProductsPage()));
                },
              ),
              ListTile(
                title: Text('Allotted Devices'),
                onTap: () {
                  // Navigator.pushReplacement(
                  // context,
                  // MaterialPageRoute(
                  // builder: (BuildContext context) =>
                  // ProductsPage()));
                },
              ),
              ListTile(
                title: Text('Assign Devices'),
                onTap: () {
                  // Navigator.pushReplacement(
                  // context,
                  // MaterialPageRoute(
                  // builder: (BuildContext context) =>
                  // ProductsPage()));
                },
              )
            ],
          ),
        ),
        appBar: AppBar(
          title: Text('Home'),
          backgroundColor:Color(0xffedac51) ,
        ),
        body: HomeBody()
    )
    );}
}
class HomeBody extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Stack(
      children: <Widget>[
        Center(
          child: new Image.asset(
            'assets/passcode.jpeg',
            width: size.width,
            height: size.height,
            fit: BoxFit.fill,
          ),
        ),
        Padding(
            padding: EdgeInsets.only(top: 200, left: 500),
            child: Column(
              children: <Widget>[
                Text("Let\'s Get Started!",
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 38.0,
                        color: Colors.white)),

              ],
            )),
      ],
    );
  }

}

当密码以ddmm格式输入为当前日期时,则应导航到下一个屏幕

1 个答案:

答案 0 :(得分:0)

不太清楚您要实现的目标,但是可以通过以下简单操作来达到目的:

DateTime today = DateTime.now();
String day, month, passCode;

if(today.day < 10){
  day = "0" + today.day.toString();
} else {
  day = today.day.toString();
}

if(today.month < 10){
  month = "0" + today.month.toString();
} else {
  month = today.month.toString();
}

passCode = day +  month;