1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Accessibility - How to Programmatically Shift Screen Reader Focus to a Specific...

Discussão em 'Mobile' iniciado por Stack, Outubro 1, 2024 às 07:12.

  1. Stack

    Stack Membro Participativo

    I am working on a Flutter project and incorporating accessibility features within a quiz. My question: How can I ensure that after a user selects an answer, the screen reader automatically focuses back on the question when the next question loads?

    I have this sample code:

    import 'package:flutter/material.dart';

    class QuizPage extends StatefulWidget {
    const QuizPage({super.key});

    @override
    _QuizPageState createState() => _QuizPageState();
    }

    class _QuizPageState extends State<QuizPage> {
    int _currentQuestionIndex = 0;
    late FocusNode _questionFocusNode;

    final List<Map<String, Object>> _questions = [
    {
    'question': 'What color is the sky?',
    'answers': ['Blue', 'Green', 'Yellow'],
    'correctAnswer': 0,
    },
    {
    'question': 'How many planets are in the solar system?',
    'answers': ['7', '8', '9'],
    'correctAnswer': 1,
    },
    {
    'question': 'Which is the largest ocean in the world?',
    'answers': ['Atlantic Ocean', 'Indian Ocean', 'Pacific Ocean'],
    'correctAnswer': 2,
    },
    {
    'question': 'What is the capital of France?',
    'answers': ['Berlin', 'Madrid', 'Paris'],
    'correctAnswer': 2,
    },
    ];

    void _answerQuestion(int selectedAnswerIndex) {
    if (selectedAnswerIndex ==
    _questions[_currentQuestionIndex]['correctAnswer']) {
    ScaffoldMessenger.of(context).showSnackBar(
    const SnackBar(content: Text('Correct answer!')),
    );
    } else {
    ScaffoldMessenger.of(context).showSnackBar(
    const SnackBar(content: Text('Wrong answer!')),
    );
    }

    setState(() {
    if (_currentQuestionIndex < _questions.length - 1) {
    _currentQuestionIndex++;
    } else {
    ScaffoldMessenger.of(context).showSnackBar(
    const SnackBar(content: Text('The quiz is finished!')),
    );
    }
    });
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: const Text('Exam'),
    ),
    body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
    Semantics(
    label: 'Frage ${_currentQuestionIndex + 1}',
    child: Text(
    _questions[_currentQuestionIndex]['question'] as String,
    style: const TextStyle(fontSize: 24),
    ),
    ),
    const SizedBox(height: 20),
    ...(_questions[_currentQuestionIndex]['answers'] as List<String>)
    .asMap()
    .entries
    .map((entry) {
    int index = entry.key;
    String answer = entry.value;
    return Semantics(
    label: 'Option ${entry.key + 1}: $answer',
    child: ElevatedButton(
    onPressed: () => _answerQuestion(index),
    child: Text(answer),
    ),
    );
    }).toList(),
    ],
    ),
    ),
    );
    }
    }

    Continue reading...

Compartilhe esta Página