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, WidgetRef ref) {
final executor = ref.watch(executorProvider);
final exceptionIndicator = _buildExceptionIndicator(context, executor);
/// Depending on screen size, show the extra actions directly as IconButtons, rather than all under a PopupMenuButton.
return LayoutBuilder(
builder: (context, constraints) {
// Calculate available width for extra actions
final runButtonWidth = 94; // "Run" FilledButton
final addFunctionWidth = onAddFunction != null
? 32
: 0; // "New Function" IconButton
final exceptionWidth = exceptionIndicator != null
? 32
: 0; // "Exception" IconButton
final padding = 8 + 16 + 48;
final availableWidth = max(
0,
constraints.maxWidth -
(runButtonWidth + addFunctionWidth + exceptionWidth + padding),
);
// If an action is directly shown as an IconButton, it will take up a width of 48.
// Here, we calculate how many such IconButtons we could fit into our availableWidth.
var maxVisibleActions = (availableWidth / 48.0).floor();
/// All extra actions
final allActions = ToolboxExtraAction.values.toList();
// Don't place a singular action under a PopupMenuButton.
if (maxVisibleActions == allActions.length - 1) {
maxVisibleActions += 1;
}
// Take the first maxVisibleActions from the full list of actions, to be shown as IconButtons.
final visibleActions = allActions.take(maxVisibleActions).toList();
// Any remaining actions should be shown under a PopupMenuButton.
final menuActions = allActions.skip(maxVisibleActions).toList();
return Container(
height: ToolboxConfig.minTouchSize,
padding: const EdgeInsets.only(left: 8),
child: Row(
children: [
/// Run button
FilledButton.icon(
onPressed: !isExecuting ? onRun : null,
icon: isExecuting
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.play_arrow),
label: const Text('Run'),
),
const SizedBox(width: 8),
/// Create New Function button
if (onAddFunction != null) ...[
InkWell(
onTap: () {
HapticFeedback.selectionClick();
onAddFunction!();
},
child: const Tooltip(
message: "Create new function",
child: NewFunctionSymbol(),
),
),
const SizedBox(width: 8),
],
/// Exception indicator
if (exceptionIndicator != null) ...[
exceptionIndicator,
const SizedBox(width: 8),
],
const Spacer(),
if (maxVisibleActions > 0) ...[
/// Extra actions as IconButtons
...visibleActions.map(
(action) => Padding(
padding: const EdgeInsets.only(right: 8),
child: IconButton(
tooltip: action == ToolboxExtraAction.dock
? isToolboxDocked
? "Undock"
: "Dock"
: action.toString(),
onPressed: () {
onTapExtraAction(action);
},
icon: Icon(
action == ToolboxExtraAction.dock
? isToolboxDocked
? Icons.open_in_new
: Icons.publish
: action.getIconData(),
),
),
),
),
/// Any remaining axtra actions under a PopupMenuButton
if (menuActions.isNotEmpty)
PopupMenuButton<ToolboxExtraAction>(
tooltip: 'More Actions',
onSelected: (action) {
onTapExtraAction(action);
},
itemBuilder: (context) => menuActions
.map(
(action) => PopupMenuItem(
value: action,
child: ListTile(
leading: Icon(
action == ToolboxExtraAction.dock
? isToolboxDocked
? Icons.open_in_new
: Icons.publish
: action.getIconData(),
),
title: Text(
action == ToolboxExtraAction.dock
? isToolboxDocked
? "Undock"
: "Dock"
: action.toString(),
),
),
),
)
.toList(),
),
],
],
),
);
},
);
}