build method
- BuildContext context,
- WidgetRef ref
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, WidgetRef ref) {
final theme = Theme.of(context);
final width = MediaQuery.sizeOf(context).width;
final isDesktop = width >= desktopBreakpoint;
// selectedIndex is null when the route is a detail screen that doesn't
// map to any of the five shell destinations. We hide the indicator in
// that case so no nav item appears falsely active.
final selectedIndex = calculateOrgSelectedIndex(context, orgId);
final highlightedIndex = selectedIndex ?? 0;
final showIndicator = selectedIndex != null;
// Deep-link safety: if the route was opened directly (no back stack —
// e.g. push notification, browser bookmark, NotificationBell using
// context.go) the default Material back button isn't rendered. Provide
// an explicit fallback that returns to the org dashboard.
final canPop = GoRouter.of(context).canPop();
// Always trail the AppBar with the mode toggle so admins can return to
// personal mode in one tap from any detail page — without it, deep
// navigation strands them inside the org shell (the bug captured in
// org-no-personal-button-android.jpeg). Caller-supplied actions appear
// before it so the toggle owns the rightmost slot.
final appBar = AppBar(
leading: canPop
? null
: IconButton(
icon: const Icon(Icons.arrow_back),
tooltip: 'Back to dashboard',
onPressed: () => context.go('/org/$orgId'),
),
title: titleWidget ?? Text(title),
actions: [...?actions, const ModeToggleButton()],
);
if (isDesktop) {
return Scaffold(
backgroundColor: theme.scaffoldBackgroundColor,
body: Row(
children: [
Semantics(
role: SemanticsRole.navigation,
label: 'Organization navigation',
container: true,
child: NavigationRail(
selectedIndex: highlightedIndex,
indicatorColor: showIndicator ? null : Colors.transparent,
onDestinationSelected: (i) => onOrgNavTapped(i, context, orgId),
labelType: NavigationRailLabelType.all,
destinations: [
for (final d in orgNavDestinations)
NavigationRailDestination(
icon: Icon(d.icon),
label: Text(d.label),
),
],
),
),
const VerticalDivider(thickness: 1, width: 1),
Expanded(
child: Scaffold(
appBar: appBar,
backgroundColor: theme.scaffoldBackgroundColor,
body: Semantics(
role: SemanticsRole.main,
label: 'Main content',
container: true,
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 1200),
child: body,
),
),
),
floatingActionButton: floatingActionButton,
),
),
],
),
);
}
return Scaffold(
backgroundColor: theme.colorScheme.surface,
appBar: appBar,
body: Semantics(
role: SemanticsRole.main,
label: 'Main content',
container: true,
child: body,
),
floatingActionButton: floatingActionButton,
bottomNavigationBar: Semantics(
role: SemanticsRole.navigation,
label: 'Organization navigation',
container: true,
child: NavigationBar(
selectedIndex: highlightedIndex,
indicatorColor: showIndicator ? null : Colors.transparent,
onDestinationSelected: (i) => onOrgNavTapped(i, context, orgId),
destinations: [
for (final d in orgNavDestinations)
NavigationDestination(icon: Icon(d.icon), label: d.label),
],
),
),
);
}