Skip to content

Instantly share code, notes, and snippets.

@huynguyennovem
Created November 22, 2022 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huynguyennovem/a33ce9373d4a7547c4a7ea7fd4456af9 to your computer and use it in GitHub Desktop.
Save huynguyennovem/a33ce9373d4a7547c4a7ea7fd4456af9 to your computer and use it in GitHub Desktop.
Dismissible date picker in Flutter
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void onPressed() {
showDatePicker(
keyboardType: TextInputType.datetime,
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1970, 1, 1),
lastDate: DateTime(2100, 12, 31),
builder: (context, child) {
return WillPopScope(child: child ?? const SizedBox.shrink(), onWillPop: () => Future.value(false),);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ElevatedButton(
onPressed: onPressed,
child: const Text('Show date picker'),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment