0

im working on a survey with multiple pages and differents kinds of questions, my problem is that the questions type dropdownButton dosnt diplay the selected answer.

I put a breakpoint in the onchanged of my dropdown

DropdownButton(
                          hint: const Text("Select answer"),
                          value: currentSelectedValue,
                          icon: const Icon(Icons.keyboard_arrow_down),
                          onChanged: (newValue) {
                            setState(() {
                             ** currentSelectedValue = newValue.toString();**
                            });
                          },
                          items: getListQuestionChoix_string(0, 2).map((_items) {
                            return DropdownMenuItem(
                                value: _items, child: Text(_items));
                          }).toList()
                        ))   

and its weird because my var "currentSelectedItem" is correctly update everytime, thats the good item before and after I choose one in my dropdown.

Here is my code:

class QuestionScreen extends StatefulWidget {
  const QuestionScreen({
    Key? key,
    required this.user,
    required this.survey,
    this.animationController,
    this.report,
    required this.categoryChoose,
  }) : super(key: key);
  final ReportEntity? report;
  final UserData user;
  final SurveyEntity survey;
  final AnimationController? animationController;
  final CategoryEntity categoryChoose;

  @override
  State<QuestionScreen> createState() => _QuestionScreenState();
}

class _QuestionScreenState extends State<QuestionScreen>
    with TickerProviderStateMixin {
  Animation<double>? topBarAnimation;
   ScrollController scrollController = ScrollController();
  double topBarOpacity = 0.0;
  PageController _pageController = PageController();
  List<Widget> _listPage = [];

   ScrollController verticalScroll = ScrollController();
   ImagePicker _picker = ImagePicker();
  //Dropdown var
  String? currentSelectedValue;
  List<DropdownButton> _listDropdown = [];
   Map<int, String> _mapDropdownAssoc= {};
  //Choices var
    List<String> _listChoices = [];
   Map<int, String> _mapChoicesAssoc= {};

 @override
  void initState() {
   _listPage.add( DropdownButton(
                          hint: const Text("Select answer"),
                          value: currentSelectedValue,
                          icon: const Icon(Icons.keyboard_arrow_down),
                          onChanged: (newValue) {
                            setState(() {
*Breakpoint here*         currentSelectedValue = newValue.toString();
                            });
                          },
                          items: getListQuestionChoix_string(0, 2).map((_items) {
                            return DropdownMenuItem(
                                value: _items, child: Text(_items));
                          }).toList()
                        ))     
  )
}

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
        body: 
        PageView(
          controller: _pageController,
          scrollDirection: Axis.horizontal,
          pageSnapping: true,
          reverse: false,
          onPageChanged: (index) {
          },
          children: _listPage,
        ),
       ),
      );
    }

Please somebody help

Breakpoint return the right value

If I put my dropdown alone in the build, everything work correctly.

MGS
  • 1

0 Answers0