Created
February 2, 2024 19:35
-
-
Save dmslabsbr/03472e876d7d2ea5a55c8f31245d3284 to your computer and use it in GitHub Desktop.
Simple Month Year Picker Widget
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
// Automatic FlutterFlow imports | |
import '/flutter_flow/flutter_flow_theme.dart'; | |
import '/flutter_flow/flutter_flow_util.dart'; | |
import '/custom_code/widgets/index.dart'; // Imports other custom widgets | |
import '/flutter_flow/custom_functions.dart'; // Imports custom functions | |
import 'package:flutter/material.dart'; | |
// Begin custom widget code | |
// DO NOT REMOVE OR MODIFY THE CODE ABOVE! | |
class MonthYearPicker extends StatefulWidget { | |
const MonthYearPicker({ | |
super.key, | |
this.width, | |
this.height, | |
required this.minDate, | |
required this.maxDate, | |
this.initialDate, | |
this.isDarkMode, | |
required this.callback, | |
}); | |
final double? width; | |
final double? height; | |
final DateTime minDate; | |
final DateTime maxDate; | |
final DateTime? initialDate; | |
final bool? isDarkMode; | |
final Future<dynamic> Function() callback; | |
// Must create app state String retWidget | |
@override | |
State<MonthYearPicker> createState() => _MonthYearPickerState(); | |
} | |
class _MonthYearPickerState extends State<MonthYearPicker> { | |
List<int> years = [2000]; | |
int selectedYear = 0; | |
String selectedMonth = ''; | |
final String mesPadrao = 'Janeiro'; | |
final int anoPadrao = 2000; | |
String mesAno = ''; | |
DateTime _initialDate = DateTime.now(); | |
bool _isDarkMode = false; | |
TextStyle estiloTexto = TextStyle( | |
color: Colors.white, | |
); | |
int _anoMinimo = 1; | |
int _anoMaximo = 2; | |
final List<String> months = <String>[ | |
'Janeiro', | |
'Fevereiro', | |
'Março', | |
'Abril', | |
'Maio', | |
'Junho', | |
'Julho', | |
'Agosto', | |
'Setembro', | |
'Outubro', | |
'Novembro', | |
'Dezembro' | |
]; | |
@override | |
void initState() { | |
super.initState(); | |
_isDarkMode = widget.isDarkMode ?? false; | |
if (_isDarkMode) { | |
estiloTexto = TextStyle( | |
color: Colors.white, | |
); | |
} else { | |
estiloTexto = TextStyle( | |
color: Colors.blue, | |
); | |
} | |
_initialDate = widget.initialDate ?? DateTime.now(); | |
_anoMinimo = widget.minDate.year; | |
_anoMaximo = widget.maxDate.year; | |
if (widget.minDate.isAfter(_initialDate)) { | |
_anoMinimo = _initialDate.year; | |
} | |
if (widget.maxDate.isBefore(_initialDate)) { | |
_anoMaximo = _initialDate.year; | |
} | |
for (int year = _anoMinimo; year <= _anoMaximo; year++) { | |
years.add(year); | |
} | |
selectedYear = _initialDate.year; | |
selectedMonth = months[_initialDate.month - 1]; | |
mesAno = '$selectedMonth/$selectedYear'; | |
print( | |
'_initialDate: $_initialDate - widget.initialDate: ${widget.initialDate} '); | |
print('MonthYearPicker: $selectedYear $selectedMonth $mesAno '); | |
} | |
@override | |
Widget build(BuildContext context) { | |
Row nRow = Row( | |
children: [ | |
DropdownButton( | |
value: selectedMonth, | |
style: estiloTexto, | |
items: months.map((String month) { | |
return DropdownMenuItem( | |
value: month, | |
child: Text(month), | |
); | |
}).toList(), | |
onChanged: (newValue) { | |
setState(() { | |
selectedMonth = newValue ?? mesPadrao; | |
FFAppState().retWidget = '$selectedMonth/$selectedYear'; | |
}); | |
mesAno = '$selectedMonth/$selectedYear'; | |
print('onChange 1'); | |
widget.callback(); | |
}, | |
), | |
DropdownButton( | |
value: selectedYear, | |
style: estiloTexto, | |
items: years.map((int year) { | |
return DropdownMenuItem( | |
value: year, | |
child: Text(year.toString()), | |
); | |
}).toList(), | |
onChanged: (newValue) { | |
setState(() { | |
selectedYear = newValue ?? anoPadrao; | |
FFAppState().retWidget = '$selectedMonth/$selectedYear'; | |
}); | |
mesAno = '$selectedMonth/$selectedYear'; | |
print('onChange 2'); | |
widget.callback(); | |
}, | |
), | |
], | |
); | |
return Container( | |
width: widget.width, | |
height: widget.height, | |
child: nRow, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment