Date Picker in Flutter





import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyDateTutorial(),
    title: "Date Picker Tutorial",
  ));
}

class MyDateTutorial extends StatefulWidget {
  @override
  _MyFieldTutorialState createState() => _MyFieldTutorialState();
}

class _MyFieldTutorialState extends State<MyDateTutorial> {
  String selecteddate = "Select Date";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Date Picker Tutorial"),
      ),
      body: Container(
        child: Column(
          children: [
            ListTile(
              title: Text("$selecteddate"),
              trailing: IconButton(
                icon: Icon(
                  Icons.date_range,
                  color: Colors.red,
                ),
               onPressed: () {
                getshowDatePicker().then((value) {
                 if (value != null) {
                   setState(() {
                    var date=DateTime.parse(value.toString());
                    selecteddate="${date.day}-${date.month}-${date.year}";
                      });
                    }
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future getshowDatePicker() async {
    var selected = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime.now(),
      lastDate: DateTime(2050),
    );
    return selected;
  }
}

No comments: