从SingleChildRenderObjectWidget到StatefulWidget的Flutter回调

hwazgwia  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(156)

我有这个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的示例。
怎么了?

相关问题