Created
November 22, 2022 07:07
-
-
Save huynguyennovem/a33ce9373d4a7547c4a7ea7fd4456af9 to your computer and use it in GitHub Desktop.
Dismissible date picker in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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