我有这个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"),
],
),
);
}
}
这个SingleChildRenderObjectWidget
发送回调并在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;
}
}
但是我不知道为什么AppointmentItem
中的onStartDateTimeChanged()
不会被触发?
我没有找到任何关于如何从SingleChildRenderObjectWidget
回调到Stfl
Widget的示例。
怎么了?
1条答案
按热度按时间ryhaxcpt1#
错误是其他类的错字,谢谢!