build method

  1. @override
Widget build(
  1. 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:

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 uiConfig = response.uiConfig ?? {};

  // Parse colors from uiConfig with defaults
  final primaryColor = UiConfigParser.parseColor(
    uiConfig['primaryColor'],
    '#6366F1', // Default indigo
  );
  final backgroundColor = UiConfigParser.parseColor(
    uiConfig['backgroundColor'],
    '#FFFFFF', // White
  );
  final textColor = UiConfigParser.parseColor(
    uiConfig['textColor'],
    '#1F2937', // Dark gray
  );
  final titleColor = uiConfig['titleColor'] != null
      ? UiConfigParser.parseColor(uiConfig['titleColor'], '#1F2937')
      : textColor;
  final buttonTextColor = UiConfigParser.parseColor(
    uiConfig['buttonTextColor'],
    '#FFFFFF', // White
  );

  // Parse other UI config values
  final buttonText = UiConfigParser.getString(
    uiConfig,
    'buttonText',
    'Update Now',
  );
  final borderRadius = UiConfigParser.getDouble(
    uiConfig,
    'borderRadius',
    16.0,
  );
  final showVersion = UiConfigParser.getBool(
    uiConfig,
    'showVersion',
    true,
  );
  final showReleaseNotes = UiConfigParser.getBool(
    uiConfig,
    'showReleaseNotes',
    true,
  );

  // Title
  final title = response.title ?? 'Update Available';

  return PopScope(
    canPop: !response.isForceUpdate,
    child: Dialog(
      backgroundColor: Colors.transparent,
      elevation: 0,
      child: Container(
        constraints: const BoxConstraints(maxWidth: 400),
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(borderRadius),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.1),
              blurRadius: 20,
              offset: const Offset(0, 10),
            ),
          ],
        ),
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // Icon with circular background
            Container(
              width: 64,
              height: 64,
              decoration: BoxDecoration(
                color: primaryColor.withOpacity(0.1),
                shape: BoxShape.circle,
              ),
              child: Icon(
                Icons.download_rounded,
                color: primaryColor,
                size: 32,
              ),
            ),
            const SizedBox(height: 16),

            // Title
            Text(
              title,
              style: TextStyle(
                color: titleColor,
                fontSize: 24,
                fontWeight: FontWeight.bold,
                height: 1.2,
              ),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 12),

            // Version (optional)
            if (showVersion && response.versionCode != null)
              Padding(
                padding: const EdgeInsets.only(bottom: 12),
                child: Text(
                  'Version ${response.versionCode}',
                  style: TextStyle(
                    color: textColor.withOpacity(0.7),
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),

            // Description / Release Notes
            if (showReleaseNotes)
              Text(
                response.releaseNotes ??
                    'A new version of the app is available. Please update to continue using the app.',
                style: TextStyle(
                  color: textColor.withOpacity(0.8),
                  fontSize: 14,
                  height: 1.5,
                ),
                textAlign: TextAlign.center,
              ),
            const SizedBox(height: 20),

            // Update button
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: onUpdate,
                style: ElevatedButton.styleFrom(
                  backgroundColor: primaryColor,
                  foregroundColor: buttonTextColor,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 24,
                    vertical: 16,
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12),
                  ),
                  elevation: 0,
                ),
                child: Text(
                  buttonText,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),

            // "Not now" button (only if not force update)
            if (!response.isForceUpdate && onCancel != null) ...[
              const SizedBox(height: 12),
              TextButton(
                onPressed: onCancel,
                style: TextButton.styleFrom(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 24,
                    vertical: 12,
                  ),
                ),
                child: Text(
                  'Not now',
                  style: TextStyle(
                    color: textColor.withOpacity(0.6),
                    fontSize: 14,
                  ),
                ),
              ),
            ],
          ],
        ),
      ),
    ),
  );
}