Skip to content

Instantly share code, notes, and snippets.

@hitchhicker007
Created October 13, 2022 12:40
Show Gist options
  • Save hitchhicker007/736b2eb57b29bf1c5f49e4f9f3f119b1 to your computer and use it in GitHub Desktop.
Save hitchhicker007/736b2eb57b29bf1c5f49e4f9f3f119b1 to your computer and use it in GitHub Desktop.
chilly-cloud-5685
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: TempScreen(),
);
}}
class TempScreen extends StatefulWidget {
const TempScreen({Key? key}) : super(key: key);
@override
State<TempScreen> createState() => _TempScreenState();
}
class _TempScreenState extends State<TempScreen> {
DateTime? selectedDate = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Calender Widget'),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
children: [
const SizedBox(
height: 50,
),
CalenderWidget(
onDateChanged: (DateTime? date) {
setState(() {
selectedDate = date;
});
},
selectedDate: selectedDate,
),
],
),
),
);
}
}
class CalenderWidget extends StatefulWidget {
final DateTime? selectedDate;
final Function(DateTime?) onDateChanged;
const CalenderWidget(
{Key? key, this.selectedDate, required this.onDateChanged})
: super(key: key);
@override
State<CalenderWidget> createState() => _CalenderWidgetState();
}
class _CalenderWidgetState extends State<CalenderWidget> {
@override
Widget build(BuildContext context) {
return Row(
children: [
SizedBox(
height: 40,
width: 40,
child: ElevatedButton(
onPressed: () {
DateTime? selectedDate =
widget.selectedDate?.subtract(const Duration(days: 1));
widget.onDateChanged.call(selectedDate);
},
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
backgroundColor: MaterialStateProperty.all(Colors.amber),
elevation: MaterialStateProperty.all(0),
),
child: const Center(
child: Icon(
Icons.arrow_back_rounded,
color: Colors.white,
),
),
),
),
const SizedBox(
width: 5,
),
Expanded(
child: InkWell(
onTap: pickDate,
child: Container(
height: 40,
decoration: BoxDecoration(
color: Colors.amber.withOpacity(.2),
borderRadius: BorderRadius.circular(5),
),
child: Center(
child: Text(
'${widget.selectedDate?.day}/${widget.selectedDate?.month}/${widget.selectedDate?.year}',
style:
const TextStyle(color: Color(0xff856513), fontSize: 18),
),
),
),
),
),
const SizedBox(
width: 5,
),
SizedBox(
height: 40,
width: 40,
child: ElevatedButton(
onPressed: () {
DateTime? selectedDate =
widget.selectedDate?.add(const Duration(days: 1));
widget.onDateChanged.call(selectedDate);
},
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
backgroundColor: MaterialStateProperty.all(Colors.amber),
elevation: MaterialStateProperty.all(0),
),
child: const Center(
child: Icon(
Icons.arrow_forward_rounded,
color: Colors.white,
),
),
),
),
],
);
}
void pickDate() async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1950),
lastDate: DateTime(2100));
if (pickedDate != null) {
DateTime? selectedDate = pickedDate;
widget.onDateChanged.call(selectedDate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment