view method
Renders the current model state for display.
This method is called after every update to refresh the screen. It should return either a String or a View object.
Guidelines
- Keep view functions pure - no side effects
- View should only depend on model state
- Use string interpolation or StringBuffer for complex views
- Consider terminal width/height for responsive layouts
Example
@override
String view() {
final buffer = StringBuffer();
// Header
buffer.writeln('╔════════════════════════════╗');
buffer.writeln('║ My Application ║');
buffer.writeln('╚════════════════════════════╝');
buffer.writeln();
// Content
if (loading) {
buffer.writeln('Loading...');
} else {
for (final item in items) {
final prefix = item == selectedItem ? '▸ ' : ' ';
buffer.writeln('$prefix$item');
}
}
buffer.writeln();
// Footer
buffer.writeln('↑/↓: Navigate Enter: Select q: Quit');
return buffer.toString();
}
Implementation
@override
String view() {
final sections = <String>[];
var availHeight = _height;
if (showTitle || (showFilter && filteringEnabled)) {
final titleView = _titleView();
sections.add(titleView);
availHeight -= titleView.split('\n').length;
}
if (showStatusBar) {
final statusView = _statusView();
sections.add(statusView);
availHeight -= 1;
}
String? pagination;
if (showPagination) {
pagination = _paginationView();
availHeight -= 1;
}
String? help;
if (showHelp) {
help = _helpView();
availHeight -= 1;
}
final content = _populatedView(availHeight);
sections.add(content);
if (pagination != null) {
sections.add(pagination);
}
if (help != null) {
sections.add(help);
}
return sections.join('\n');
}