TimedAccessGuard class

A widget that conditionally renders its child based on a specified time window.

TimedAccessGuard allows you to control access to a portion of the UI based on a fixed time range (start to end). This is useful for features that should only be visible or active during a specific period—such as flash sales, scheduled promotions, live events, or timed maintenance windows.

The widget evaluates access periodically using a configurable checkInterval (default is 1 second), making it reactive to time updates in real-time.

Example:

TimedAccessGuard(
  start: DateTime(2025, 7, 19, 10, 0),
  end: DateTime(2025, 7, 19, 18, 0),
  builder: (_) => Text("Access allowed"),
  fallbackBuilder: (_) => Text("Access denied"),
)

Parameters:

  • start: The beginning of the access window (inclusive).
  • end: The end of the access window (exclusive).
  • builder: The widget to render if current time is within start and end.
  • fallbackBuilder: An optional widget to show outside the time window. If not provided, an empty box is shown.
  • checkInterval: Time interval to evaluate whether current time is still within the range. Defaults to 1 second.
  • onTimeUpdate: Optional callback that receives the remaining time (until end) on each evaluation.

Behavior:

  • If current time is within the window: builder is rendered.
  • If current time is before or after the window: fallbackBuilder or SizedBox.shrink() is shown.
  • onTimeUpdate is invoked after each tick with remaining time (zero if expired).

Notes:

  • Time comparisons use .isAfter(start) and .isBefore(end) — i.e., the range is (start, end).
  • If now == start, the builder will not be shown until the clock ticks forward.
  • fallbackBuilder can be used to indicate expired or inactive state.

Use Cases:

  • Temporarily enable/disable sections of UI based on time
  • Maintenance banners or blackouts
  • Scheduled releases or countdowns
  • Time-limited access to premium features or rewards
Inheritance

Constructors

TimedAccessGuard({Key? key, required DateTime start, required DateTime end, required WidgetBuilder builder, WidgetBuilder? fallbackBuilder, Duration checkInterval = const Duration(seconds: 1), void onTimeUpdate(Duration timeLeft)?})
const

Properties

builder WidgetBuilder
final
checkInterval Duration
final
end DateTime
final
fallbackBuilder WidgetBuilder?
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onTimeUpdate → void Function(Duration timeLeft)?
Optional callback to send the remaining time on each update.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
start DateTime
final

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
override
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited