|  | 
|  | 1 | +import 'package:flutter/cupertino.dart'; | 
|  | 2 | +import 'time_to_initial_display_tracker.dart'; | 
|  | 3 | + | 
|  | 4 | +import '../frame_callback_handler.dart'; | 
|  | 5 | + | 
|  | 6 | +/// A widget that reports the Time To Initially Displayed (TTID) of its child widget. | 
|  | 7 | +/// | 
|  | 8 | +/// This widget wraps around another widget to measure and report the time it takes | 
|  | 9 | +/// for the child widget to be initially displayed on the screen. This method | 
|  | 10 | +/// allows a more accurate measurement than what the default TTID implementation | 
|  | 11 | +/// provides. The TTID measurement begins when the route to the widget is pushed and ends | 
|  | 12 | +/// when `addPostFramecallback` is triggered. | 
|  | 13 | +/// | 
|  | 14 | +/// Wrap the widget you want to measure with [SentryDisplayWidget], and ensure that you | 
|  | 15 | +/// have set up Sentry's routing instrumentation according to the Sentry documentation. | 
|  | 16 | +/// | 
|  | 17 | +/// ```dart | 
|  | 18 | +/// SentryDisplayWidget( | 
|  | 19 | +///   child: MyWidget(), | 
|  | 20 | +/// ) | 
|  | 21 | +/// ``` | 
|  | 22 | +/// | 
|  | 23 | +/// Make sure to configure Sentry's routing instrumentation in your app by following | 
|  | 24 | +/// the guidelines provided in Sentry's documentation for Flutter integrations: | 
|  | 25 | +/// https://docs.sentry.io/platforms/flutter/integrations/routing-instrumentation/ | 
|  | 26 | +/// | 
|  | 27 | +/// See also: | 
|  | 28 | +/// - [Sentry's documentation on Flutter integrations](https://docs.sentry.io/platforms/flutter/) | 
|  | 29 | +///   for more information on how to integrate Sentry into your Flutter application. | 
|  | 30 | +class SentryDisplayWidget extends StatefulWidget { | 
|  | 31 | +  final Widget child; | 
|  | 32 | +  final FrameCallbackHandler _frameCallbackHandler; | 
|  | 33 | + | 
|  | 34 | +  SentryDisplayWidget({ | 
|  | 35 | +    super.key, | 
|  | 36 | +    required this.child, | 
|  | 37 | +    @visibleForTesting FrameCallbackHandler? frameCallbackHandler, | 
|  | 38 | +  }) : _frameCallbackHandler = | 
|  | 39 | +            frameCallbackHandler ?? DefaultFrameCallbackHandler(); | 
|  | 40 | + | 
|  | 41 | +  @override | 
|  | 42 | +  _SentryDisplayWidgetState createState() => _SentryDisplayWidgetState(); | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +class _SentryDisplayWidgetState extends State<SentryDisplayWidget> { | 
|  | 46 | +  @override | 
|  | 47 | +  void initState() { | 
|  | 48 | +    super.initState(); | 
|  | 49 | +    TimeToInitialDisplayTracker().markAsManual(); | 
|  | 50 | + | 
|  | 51 | +    widget._frameCallbackHandler.addPostFrameCallback((_) { | 
|  | 52 | +      TimeToInitialDisplayTracker().completeTracking(); | 
|  | 53 | +    }); | 
|  | 54 | +  } | 
|  | 55 | + | 
|  | 56 | +  @override | 
|  | 57 | +  Widget build(BuildContext context) { | 
|  | 58 | +    return widget.child; | 
|  | 59 | +  } | 
|  | 60 | +} | 
0 commit comments