How do I pass down the data from the datepicker to the _addCashButton
widget?
I was able to assign the date to newDateTime
. I assigned it again to addDate
to save to the database but it's coming up as null
class AddCashTab extends StatefulWidget {
const AddCashTab({
Key key,
}) : super(key: key);
@override
_AddCashTabState createState() => _AddCashTabState();
}
class _AddCashTabState extends State<AddCashTab> {
DateTime newDateTime;
DateTime date = DateTime.now();
@override
void initState() {
super.initState();
controller = TextEditingController();
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_addDateButton(context),
_addCashButton(context),
],
),
);
}
Widget _addDateButton(BuildContext context) {
return GestureDetector(
onTap: () {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) {
return _buildBottomPicker(
CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: date,
onDateTimeChanged: (DateTime newDateTime) {
setState(() {
date = newDateTime;
print(newDateTime);
});
},
),
);
},
);
},
child: _buildMenu(
<Widget>[
// CupertinoButton.filled(
// child: Text('Select Date'),
// ),
const Text('Select Date'),
Text(
DateFormat.yMMMMd().format(date),
style: const TextStyle(color: CupertinoColors.inactiveGray),
),
],
),
);
}
Widget _addCashButton(BuildContext context) {
return CupertinoButton.filled(
child: Text('Add Cash'),
onPressed: () {
final database = Provider.of<AppDatabase>(context);
final task = AddCashFlow(
addName: controller.text,
addDate: newDateTime,
);
database.insertAddCash(task);
},
);
}
}