英文:
Flutter Callback from SingleChildRenderObjectWidget to StatefulWidget
问题
AppointmentItem
's onStartDateTimeChanged()
method is not being triggered because you have a naming mismatch. In your AppointmentItem
class, you defined the callback as onStartDateTimeChanged
, but you're trying to call it as a regular method without parameters. You should use the callback correctly like this:
onStartDateTimeChanged?.call(widget.starttime);
This will correctly invoke the callback when needed.
Regarding making a callback from a SingleChildRenderObjectWidget
to a StatefulWidget
, the code you've provided seems correct in terms of passing the callback from TimelineItem
to AppointmentItem
. Make sure you're correctly passing the onStartDateTimeChanged
callback when you create a TimelineItem
. If the callback still doesn't work, check for any other potential issues in your code or ensure that the parent widget is correctly rebuilding when the callback should trigger.
英文:
I have this StatefulWidget
:
class AppointmentItem extends StatefulWidget {
const AppointmentItem({Key? key, required this.starttime, required this.onStartDateTimeChanged}) : super(key: key);
final DateTime starttime;
final void Function(DateTime)? onStartDateTimeChanged;
@override
State<AppointmentItem> createState() => _AppointmentItemState();
}
class _AppointmentItemState extends State<AppointmentItem> {
onStartDateTimeChanged() {
setState(() {
print("hallo CB");
});
}
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
color: Colors.amberAccent,
borderRadius: BorderRadius.circular(10),
),
child: Column(
children: [
Row(
children: [
Text(widget.starttime.toString()),
],
),
Text("hallo"),
],
),
);
}
}
and this SingleChildRenderObjectWidget
that sends a callback and gets a callback on onStartDateTimeUpdated
:
class TimelineItem extends SingleChildRenderObjectWidget {
TimelineItem({
Key? key,
required this.startDateTime,
required this.endDateTime,
this.position = 0,
this.onStartDateTimeChanged,
this.onEndDateTimeChanged,
this.onStartDateTimeUpdated,
this.onEndDateTimeUpdated,
}) : assert(
startDateTime.isBefore(endDateTime),
'startDateTime must be before endDateTime: '
'starDateTime: $startDateTime --- endDateTime: $endDateTime',
),
super(
key: key,
child: AppointmentItem(starttime: startDateTime, onStartDateTimeChanged: onStartDateTimeChanged,),
);
final DateTime startDateTime;
final DateTime endDateTime;
final int position;
final void Function(DateTime)? onStartDateTimeChanged;
final void Function(DateTime)? onEndDateTimeChanged;
final void Function(DateTime)? onStartDateTimeUpdated;
final void Function(DateTime)? onEndDateTimeUpdated;
@override
RenderObject createRenderObject(BuildContext context) {
return RenderTimelineItem(
startDateTime: startDateTime,
endDateTime: endDateTime,
position: position,
onStartDateTimeUpdated: onStartDateTimeUpdated,
onEndDateTimeUpdated: onEndDateTimeUpdated,
onStartDateTimeChanged: onStartDateTimeChanged,
onEndDateTimeChanged: onEndDateTimeChanged,
);
}
@override
void updateRenderObject(
BuildContext context,
covariant RenderTimelineItem renderObject,
) {
renderObject
..startDateTime = startDateTime
..endDateTime = endDateTime
..position = position
..onStartDateTimeUpdated = onStartDateTimeUpdated
..onEndDateTimeUpdated = onEndDateTimeUpdated
..onStartDateTimeChanged = onStartDateTimeChanged
..onEndDateTimeChanged = onEndDateTimeChanged;
}
}
But I dont get the clue why onStartDateTimeChanged()
in AppointmentItem
will not be triggered?
I did not found any example on how to make a callback from a SingleChildRenderObjectWidget
to a Stfl
Widget.
What is wrong?
答案1
得分: 0
"Error was typo in other class,
thank you!" 可以翻译为: "错误是其他类中的拼写错误,谢谢!"
英文:
Error was typo in other class,
thank you!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论