build method
Widget build (BuildContext context)
override
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
context
using 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 navigationServiceLocal = locator<NavigationService>();
model.organizations = [];
int noOfRefetch = 0;
return GraphQLProvider(
client: ValueNotifier<GraphQLClient>(graphqlConfig.clientToQuery()),
child: Query(
options: QueryOptions(
document: gql(queries.fetchJoinInOrg),
variables: {
// fetch 15 items only, will fetch more when scrolling index is at the 3rd last item!
'first': 15,
'skip': 0,
},
),
builder: (
QueryResult result, {
Future<QueryResult> Function(FetchMoreOptions)? fetchMore,
Future<QueryResult?> Function()? refetch,
}) {
// checking for any errors, if true fetch again!
if (result.hasException) {
final isException = databaseFunctions.encounteredExceptionOrError(
result.exception!,
showSnackBar: noOfRefetch == 0,
);
if (isException != null) {
if (isException) {
refetch!();
noOfRefetch++;
} else {
refetch!();
noOfRefetch++;
}
}
} else {
// If the result is still loading!
if (!result.isLoading) {
model.organizations = OrgInfo().fromJsonToList(
result.data!['organizationsConnection'] as List,
);
}
Timer(const Duration(seconds: TimeOuts.small), () {
if (model.organizations.isEmpty) {
navigationServiceLocal.showTalawaErrorDialog(
"No organizations found Please contact your admin",
MessageType.error,
);
}
});
return Scrollbar(
thumbVisibility: true,
interactive: true,
controller: model.allOrgController,
// Listview is a scrollable list of widgets arranged linearly.
child: ListView.separated(
controller: model.allOrgController,
padding: EdgeInsets.zero,
shrinkWrap: true,
itemCount: result.isLoading
? model.organizations.length + 1
: model.organizations.length,
itemBuilder: (BuildContext context, int index) {
// If the index is at the end of the list!
if (index == model.organizations.length) {
// return the ListTile showing the loading icon!
return ListTile(
title: Center(
child: CupertinoActivityIndicator(
radius: SizeConfig.screenWidth! * 0.065,
),
),
);
}
// If the index is at the 3rd last item in the organization list.
if (index == model.organizations.length - 3) {
// return VisibilityDetector and fetch more items in the list to show up!
return VisibilityDetector(
key: const Key('OrgSelItem'),
onVisibilityChanged: (VisibilityInfo info) {
if (info.visibleFraction > 0) {
model.fetchMoreHelper(
fetchMore!,
model.organizations,
);
}
},
child: CustomListTile(
index: index,
type: TileType.org,
orgInfo: model.organizations[index],
onTapOrgInfo: (item) => model.selectOrg(item),
key: Key('OrgSelItem$index'),
),
);
}
// return CustomeTile that shows a particular item in the list!
return CustomListTile(
index: index,
type: TileType.org,
orgInfo: model.organizations[index],
onTapOrgInfo: (item) => model.selectOrg(item),
key: Key('OrgSelItem$index'),
);
},
separatorBuilder: (BuildContext context, int index) {
return Container();
},
),
);
}
return Container();
},
),
);
}