Static Functions
Overview
Static functions are available in every EL expression without additional setup. They accept
expressions as arguments, so you can compose them — length(replaceAll(user.name, ' ', ''))
works the same as any other nested call. See Rules for the full EL evaluation model.
XWidget also registers a small set of application functions during
XWidget.initialize(). Those functions are not part of the core xwidget_el
package, but they are available in XWidget fragments after initialization. See
XWidget-Registered Functions.
Functions
abs
Absolute value.
ceil
Round up to nearest integer.
clamp
Clamp a value to a range. Returns lower if value is below the range,
upper if above, otherwise returns the value unchanged. All values must
be [Comparable].
contains
Check if a value contains a search value.
<if test="${contains('Hello, World!', 'World')}">
<!-- true condition -->
<else>
<!-- optional false condition -->
</else>
</if>
containsKey
Check if a map contains a key.
containsValue
Check if a map contains a value.
diffDateTime
Calculate duration between two dates.
endsWith
Check if a string ends with a value.
eval
Evaluate an expression string.
first
Get first element of a collection.
floor
Round down to nearest integer.
formatBytes
Format a byte count into a human-readable string with unit suffix (B, KB, MB, GB, TB).
formatCompact
Format a number with a compact suffix (K, M, B, T). Trailing zeros are removed.
formatDateTime
Format a DateTime with a pattern.
formatDuration
Format a Duration with specified precision.
String? formatDuration(Duration? value, [String precision = "s", DurationFormat? format = defaultDurationFormat]);
formatElapsed
Format a DateTime as a relative time string (e.g. "5 minutes ago", "in 3 hours"). Returns "just now" for durations under 60 seconds in the past, and "in a moment" for durations under 60 seconds in the future.
formatNumber
Format a number using an ICU/intl pattern string. Supports grouping separators, decimal places, percentages, and currency symbols.
<Text data="${formatNumber(totalRenders, '#,##0')}"/>
<!-- 24891 -> "24,891" -->
<Text data="${formatNumber(price, '#,##0.00')}"/>
<!-- 3.14159 -> "3.14" -->
formatOrdinal
Format an integer with an English ordinal suffix (st, nd, rd, th). Correctly handles teen exceptions (11th, 12th, 13th).
formatPercent
Format a number as a percentage string. Multiplies the value by 100 and appends '%'.
formatPlural
Format a count with a singular or plural label. Uses the singular form when the absolute value of count is 1.
<Text data="${formatPlural(errorCount, 'error', 'errors')}"/>
<!-- 0 -> "0 errors", 1 -> "1 error", 5 -> "5 errors" -->
isBlank
Check if a value is blank (null, empty, or whitespace).
isEmpty
Check if a value is empty.
<if test="${isEmpty(myList)}">
<!-- true condition -->
<else>
<!-- optional false condition -->
</else>
</if>
isFalse
Check if a value evaluates to boolean false. Uses toBool for conversion.
isFalseOrNull
Check if a value is false or null.
isNotBlank
Check if a value is not blank.
isNotEmpty
Check if a value is not empty.
isNotNull
Check if a value is not null.
isNull
Check if a value is null.
isTrue
Check if a value evaluates to boolean true. Uses toBool for conversion.
isTrueOrNull
Check if a value is true or null.
last
Get last element of a collection.
length
Get length of a collection or string.
logDebug
Log a debug message.
<!-- expressions evaluate at inflation time; use <callback> to defer to the tap -->
<Button>
<callback for="onPressed" action="logDebug('Button clicked')"/>
<Text data="Click me"/>
</Button>
matches
Check if a string matches a regular expression. The entire string must match —
a pattern that matches only part of the value returns false, so contains-style
partial matching needs explicit wildcards (e.g. '.*ing').
max
Return the larger of two values. Both values must be [Comparable]. If either value is null, the non-null value is returned.
min
Return the smaller of two values. Both values must be [Comparable]. If either value is null, the non-null value is returned.
now
Get current DateTime.
nowUtc
Get current DateTime in UTC.
randomDouble
Generate a random double between 0.0 and 1.0.
randomInt
Generate a random integer.
replaceAll
Replace all occurrences in a string.
replaceFirst
Replace first occurrence in a string.
round
Round to nearest integer.
startsWith
Check if a string starts with a value.
<if test="${startsWith('Dart is fun', 'Dart')}">
<!-- true condition -->
<else>
<!-- optional false condition -->
</else>
</if>
substring
Extract a substring.
toBool
Convert to boolean.
toColor
Convert to Color.
toDateTime
Convert to DateTime.
toDays
Convert to days.
toDouble
Convert to double.
toDuration
Convert to Duration.
toHours
Convert to hours.
toInt
Convert to integer.
toMillis
Convert to milliseconds.
toMinutes
Convert to minutes.
toSeconds
Convert to seconds.
toString
Convert to string.
tryToBool
Try to convert to boolean, returns null on failure.
tryToColor
Try to convert to Color, returns null on failure.
tryToDateTime
Try to convert to DateTime, returns null on failure.
tryToDays
Try to convert to days, returns null on failure.
tryToDouble
Try to convert to double, returns null on failure.
tryToDuration
Try to convert to Duration, returns null on failure.
tryToHours
Try to convert to hours, returns null on failure.
tryToInt
Try to convert to integer, returns null on failure.
tryToMillis
Try to convert to milliseconds, returns null on failure.
tryToMinutes
Try to convert to minutes, returns null on failure.
tryToSeconds
Try to convert to seconds, returns null on failure.
XWidget-Registered Functions
The functions in this section are registered by XWidget during
XWidget.initialize(). Use them in fragments when you need access to XWidget
resources, navigation helpers, or the global navigator key.
resBool
Returns a boolean value resource by name.
resColor
Returns a color value resource by name.
resColorString
Returns a color value resource as a string.
resDouble
Returns a double value resource by name.
resInt
Returns an integer value resource by name.
resString
Returns a string value resource by name.
For static attribute values, prefer resource directives such as
@string/usage_title or @color/accent. Use the res* functions when an
attribute needs an expression or computed value. See
Resources for resource declarations and lookup
behavior.
routeTo
Returns a callback that navigates to a route by path or name. Use it in XML
attributes such as onPressed or onTap.
<TextButton onPressed="${routeTo('/settings')}">
<Text data="Settings" />
</TextButton>
<ElevatedButton onPressed="${routeTo('/login', 'pushAndRemoveAll')}">
<Text data="Log Out" />
</ElevatedButton>
The optional action argument is parsed as a NavigatorAction value. If it is
omitted or invalid, XRouter uses NavigatorAction.push.
routePop
Returns a callback that pops the current Navigator route.
routePopAll
Returns a callback that pops Navigator routes until the root route remains.
routeTo, routePop, and routePopAll operate through XRouter and
XWidget.navigatorKey. See Routing for route
definitions, callback routes, browser history, and Navigator actions.
navigatorKey
Returns XWidget's global navigator key. Use it when assigning
navigatorKey on the root app widget.
<MaterialApp
xmlns="https://xwidget.dev/fragments"
navigatorKey="${navigatorKey()}">
...
</MaterialApp>
See the Navigator Key section for setup details.