build method
- BuildContext context
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) {
return Scaffold(
resizeToAvoidBottomInset: true,
// Loads the diary entry by ID using a FutureBuilder.
body: FutureBuilder<DiaryEntry?>(
future: DiaryService().getEntryById(entryId),
builder: (context, snapshot) {
// Show loading spinner while waiting for data.
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
// Show error message if the request failed.
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
final entry = snapshot.data;
if (entry == null) {
return const Center(child: Text('Page not found.'));
}
// Change status bar appearance.
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Color(0xFF95A86E),
statusBarIconBrightness: Brightness.light,
),
);
// Build the layout once data is available.
return SafeArea(
child: LayoutBuilder(
builder: (context, constraints) {
return SingleChildScrollView(
physics: const ClampingScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: constraints.maxHeight,
),
child: Container(
padding: const EdgeInsets.all(16),
color: const Color(0xFF95A86E),
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFF6E1C4),
borderRadius: BorderRadius.circular(10),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Header with back button and app title.
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(
Icons.arrow_back_rounded,
size: 30,
),
color: const Color(0xFF2E5355),
onPressed: () => onBack(),
),
Expanded(
child: Center(
child: Text(
'Itinerèo',
style: GoogleFonts.libreBaskerville(
fontSize: 20,
fontWeight: FontWeight.bold,
color: const Color(0xFFD28F3F),
height: 1.3,
),
),
),
),
Icon(
Icons.photo_album_outlined,
color: const Color(0xFF2E5355),
size: 35,
),
],
),
// Title of the diary entry.
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 10,
),
decoration: BoxDecoration(
color: const Color(0xFFF6ECD4),
border: Border.all(
color: const Color(0xFFD8CCB1),
width: 4,
),
),
child: Column(
children: [
Text(
'"${entry.title}"',
style: GoogleFonts.deliciousHandrawn(
textStyle: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.w600,
color: Color(0xFF2E5355),
),
),
textAlign: TextAlign.center,
),
],
),
),
const SizedBox(height: 10),
// Photo carousel if there are photos.
if (entry.photoUrls.isNotEmpty)
PhotoCarousel(
photoUrls: entry.photoUrls,
controller: _pageController,
caption:
'${_formatDate(entry.date)} • ${entry.location}',
maxPhotos: 5,
),
// Description box.
Container(
width: double.infinity,
margin: const EdgeInsets.symmetric(vertical: 8.0),
padding: const EdgeInsets.all(16.0),
constraints: const BoxConstraints(minHeight: 120),
decoration: BoxDecoration(
color: const Color(0xFFFFF5DD),
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(
color: Colors.black12,
blurRadius: 6,
offset: Offset(0, 3),
),
],
),
alignment: Alignment.center,
child: Text(
'« ${entry.description} »',
style: GoogleFonts.playpenSans(
fontSize: 13,
fontWeight: FontWeight.w500,
),
textAlign: TextAlign.justify,
),
),
],
),
),
),
),
);
},
),
);
},
),
);
}