build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
final abstractConfiguration = AbstractConfiguration.of(
context,
); // Gets configuration
final mainChild = AbstractStatefulBuilder(
// Initializes the state of the child on first load
initState: (context) {
if (!skipInitialOnInit) {
_onInit(context); // Calls _onInit if the initial call is not skipped
}
},
// Handles the state changes in the child widget
builder: (context) => BlocConsumer<B, S>(
listener: (context, state) {
listener?.call(
context,
state,
); // Invokes the listener for state changes
// Handles different loading states and their respective callbacks
if (state.resultStatus == ResultStatus.loaded) {
onLoaded?.call(context, state);
}
if (state.resultStatus == ResultStatus.loadedCached) {
onLoadedCached?.call(context, state);
}
if (state.resultStatus == ResultStatus.error) {
onError?.call(context, state);
}
},
builder: (context, state) {
// Displays the loader if still loading and no data is present
if (_isLoading(context, state) && _isEmpty(context, state)) {
return loaderBuilder?.call(context, state) ??
abstractConfiguration?.loaderBuilder?.call(context) ??
const Loader();
}
// Handles the case where there is no data available
if (_isEmpty(context, state)) {
if (_isError(context, state)) {
return errorBuilder?.call(
context,
() => _onInit(context),
state,
) ??
AbstractConfiguration.of(context)?.abstractItemErrorBuilder
?.call(context, () => _onInit(context)) ??
AbstractItemErrorContainer(onInit: () => _onInit(context));
} else {
return noDataBuilder?.call(
context,
() => _onInit(context),
state,
) ??
AbstractConfiguration.of(context)?.abstractItemNoDataBuilder
?.call(context, () => _onInit(context)) ??
AbstractItemNoDataContainer(onInit: () => _onInit(context));
}
}
// Displays the main content when data is present
return Stack(
children: [
child ?? builder?.call(context, state) ?? Container(),
// Displays cached data warning if enabled
if (showCachedDataWarningIcon)
Positioned(
top: 0,
right: 0,
child: LoadInfoIcon(
isLoading: _isLoading(
context,
state,
), // Passes loading state
isCached: _isCached(context, state), // Passes cached state
onReload: (_) =>
_onInit(context), // Reloads data on request
),
),
],
);
},
),
);
// Provides the BLoC/Cubit as needed to the widget tree
if (providerValue != null) {
return BlocProvider.value(value: providerValue!, child: mainChild);
}
if (provider != null) {
return BlocProvider<B>(create: provider!, child: mainChild);
}
if (providers != null && providers!.isNotEmpty) {
return MultiBlocProvider(providers: providers!, child: mainChild);
}
return mainChild; // Returns the main child if no providers are set
}