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) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Face Liveness Examples'),
    ),
    body: SingleChildScrollView(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const Text(
            'Select a liveness detection example:',
            style: TextStyle(fontSize: 18),
          ),
          const SizedBox(height: 24),
          _buildExampleButton(
            context,
            'Default Style',
            'Use the package with default settings',
            () => _navigateToLivenessScreen(
              context,
              const LivenessConfig(),
              const LivenessTheme(),
            ),
          ),
          _buildExampleButton(
            context,
            'Custom Theme',
            'Custom colors and styling',
            () => _navigateToLivenessScreen(
              context,
              const LivenessConfig(),
              const LivenessTheme(
                primaryColor: Colors.purple,
                ovalGuideColor: Colors.purpleAccent,
                successColor: Colors.green,
                errorColor: Colors.redAccent,
                overlayOpacity: 0.6,
                progressIndicatorColor: Colors.purpleAccent,
                instructionTextStyle: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
                useOvalPulseAnimation: true,
              ),
            ),
          ),
          _buildExampleButton(
            context,
            'Custom Challenges',
            'Specific challenge sequence with custom messages',
            () {
              const customConfig = LivenessConfig(
                initialZoomFactor: 0.1,
                challengeTypes: [
                  ChallengeType.blink,
                  ChallengeType.zoom,
                  ChallengeType.turnRight,
                  ChallengeType.turnLeft,
                  ChallengeType.tiltUp,
                  ChallengeType.tiltDown,
                  ChallengeType.smile,
                  ChallengeType.normal,
                  //ChallengeType.nod,
                ],
                challengeInstructions: {
                  ChallengeType.blink: 'Blink your eyes slowly',
                  ChallengeType.zoom: 'Bring your face closer slowly',
                  ChallengeType.turnRight: 'Turn your head to the right',
                  ChallengeType.turnLeft: 'Turn your head to the left',
                  ChallengeType.tiltUp: 'Tilt up your head',
                  ChallengeType.tiltDown: 'Tilt down your head',
                  ChallengeType.smile: 'Show me your best smile',
                  ChallengeType.normal: 'Center Your Face',
                  //ChallengeType.nod: 'Nod your head up and down',
                },
              );
              _navigateToLivenessScreen(
                  context, customConfig, const LivenessTheme(
                  successColor: Colors.green,
                  errorColor: Colors.redAccent,
                  guidanceTextStyle: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.w500,
                  ),
              ));
            },
          ),
          _buildExampleButton(
            context,
            'Custom Challenges and Status Messages',
            'Specific challenge sequence with custom messages',
                () {
              const customConfig = LivenessConfig(
                initialZoomFactor: 0.1,
                challengeTypes: [
                  ChallengeType.blink,
                  ChallengeType.zoom,
                  ChallengeType.turnRight,
                  ChallengeType.turnLeft,
                  ChallengeType.tiltUp,
                  ChallengeType.tiltDown,
                  ChallengeType.smile,
                  ChallengeType.normal,
                  //ChallengeType.nod,
                ],
                challengeInstructions: {
                  ChallengeType.blink: 'Pisque os olhos lentamente',
                  ChallengeType.zoom: 'Aproxime o rosto lentamente',
                  ChallengeType.turnRight: 'Vire a cabeça para o lado direito',
                  ChallengeType.turnLeft: 'Vire a cabeça para o lado esquerdo',
                  ChallengeType.tiltUp: 'Incline a cabeça para cima',
                  ChallengeType.tiltDown: 'Incline a cabeça para baixo',
                  ChallengeType.smile: 'Mostre seu melhor sorriso',
                  ChallengeType.normal: 'Centralize seu rosto',
                  //ChallengeType.nod: 'Acene com a cabeça',
                },
                messages: LivenessMessages(
                  moveFartherAway: 'Afaste-se um pouco',
                  moveCloser: 'Aproxime-se um pouco',
                  moveLeft: 'Mova para a esquerda',
                  moveRight: 'Mova para a direita',
                  moveUp: 'Mova para cima',
                  moveDown: 'Mova para baixo',
                  perfectHoldStill: 'Perfeito! Fique parado',
                  noFaceDetected: 'Nenhum rosto detectado',
                  errorCheckingFacePosition: 'Ocorreu um erro no processamento',

                  initializing: 'Inicializando...',
                  initializingCamera: 'Inicializando a camera...',
                  errorInitializingCamera: 'Erro ao inicializar a câmera. Reinicie o aplicativo.',
                  initialInstruction: 'Posicione seu rosto no oval',
                  poorLighting: 'Mova-se para uma área mais iluminada',
                  processingVerification: 'Processando verificação...',
                  verificationComplete: 'Verificação de vivacidade concluída!',
                  spoofingDetected: "Possível falsificação detectada: rosto movido, mas dispositivo não",
                  errorProcessing: 'Ocorreu um erro de processamento',
                ),
              );
              _navigateToLivenessScreen(
                  context, customConfig, const LivenessTheme(
                successColor: Colors.green,
                errorColor: Colors.redAccent,
                guidanceTextStyle: TextStyle(
                  //color: Color(0xFF2E38B7),
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
                //overlayColor: Colors.white,
                // overlayOpacity: 0.85,
                // primaryColor: Colors.white,
                // ovalGuideColor: Colors.white, //.withValues(alpha: 0.87),
                // appBarBackgroundColor: Colors.white,
                // appBarTextColor: Colors.black87
              ));
            },
          ),
          _buildExampleButton(
            context,
            'Material Design',
            'Theme based on Material Design',
            () {
              final materialTheme = LivenessTheme.fromMaterialColor(
                Colors.teal,
                brightness: Brightness.dark,
              );
              _navigateToLivenessScreen(
                  context, const LivenessConfig(), materialTheme);
            },
          ),
          _buildExampleButton(
            context,
            'Capture User Image',
            'Take a photo after successful verification',
            () => _navigateToLivenessWithImageCapture(context),
          ),
          _buildExampleButton(
            context,
            'Custom Configuration',
            'Modified thresholds and settings',
            () {
              const customConfig = LivenessConfig(
                maxSessionDuration: Duration(minutes: 3),
                eyeBlinkThresholdOpen: 0.8,
                eyeBlinkThresholdClosed: 0.2,
                smileThresholdSmiling: 0.8,
                headTurnThreshold: 15.0,
                ovalHeightRatio: 0.7,
                ovalWidthRatio: 0.8,
                strokeWidth: 5.0,
                numberOfRandomChallenges: 2,
              );
              _navigateToLivenessScreen(
                  context, customConfig, const LivenessTheme());
            },
          ),
        ],
      ),
    ),
  );
}