Untuk membuat dan menampilkan inputan tanggal (date picker) pada Flutter, Anda dapat menggunakan widget showDatePicker
yang disediakan oleh Flutter. Berikut adalah contoh cara membuat inputan tanggal:
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DatePickerExample(),
);
}
}
class DatePickerExample extends StatefulWidget {
@override
_DatePickerExampleState createState() => _DatePickerExampleState();
}
class _DatePickerExampleState extends State {
DateTime selectedDate = DateTime.now();
Future _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2000),
lastDate: DateTime(2101),
);
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Input Tanggal’),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
“${selectedDate.toLocal()}”.split(‘ ‘)[0],
style: TextStyle(fontSize: 55, fontWeight: FontWeight.bold),
),
SizedBox(
height: 20.0,
),
ElevatedButton(
onPressed: () => _selectDate(context),
child: Text(‘Pilih Tanggal’),
),
],
),
),
);
}
}