如何在Flutter中增加列表视图的滚动位置

时间:2019-05-31 06:55:57

标签: flutter dart

我正在开发一个应用程序,在该应用程序中我需要解析json,然后逐个显示一个问题。在问题下方,我有一个按钮next,它将列表视图跳至下一个位置并显示下一个问题。

{
    "error": false,
    "response": [
        {
            "ques_id": 5,
            "quiz_questions": "Does your friend constantly have to account for there whereabouts?",
            "option_1": "Yes",
            "option_2": "No"
        },
        {
            "ques_id": 6,
            "quiz_questions": "Does your friend constantly have to account for there whereabouts?",
            "option_1": "Yes",
            "option_2": "No"
        },
        {
            "ques_id": 7,
            "quiz_questions": "Does your friend constantly have to account for there whereabouts?",
            "option_1": "Yes",
            "option_2": "No"
        },
        {
            "ques_id": 8,
            "quiz_questions": "sdfsdfsdfsdfsdfsdfsdf",
            "option_1": "Yes",
            "option_2": "No"
        }
    ]
}

我需要解析json之上。 现在,这是我的列表视图颤动代码。

 child: ListView.builder(
                  itemCount: question_list.length,
                  physics: const NeverScrollableScrollPhysics(),
                  scrollDirection: Axis.horizontal,
                  shrinkWrap: true,
                  controller: _scrollController,
                  itemBuilder:(context, position){
                    return Container(
                      width:MediaQuery.of(context).size.width * 1,
                      height: MediaQuery.of(context).size.height *0.9,
                      child: Column(
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.all(10),
                            padding: EdgeInsets.all(20),
                            child:Text(question_list[position].quiz_question,
                              style: TextStyle(fontSize: 18.0,color: Colors.white),),
                          ),

                          Column(
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Radio(
                                    value:1,
                                    groupValue: _radioValue,
                                    onChanged: _handleRadioValueChange,
                                    activeColor: Colors.blue,
                                  ),
                                  Container(
                                    margin: EdgeInsets.all(10),

                                    child:Text(question_list[position].option_1,
                                      style: TextStyle(fontSize: 18.0,color: Colors.white),),
                                  ),
                                ],
                              ),

                              Row(
                                children: <Widget>[
                                  Radio(
                                    value: 0,
                                    groupValue: _radioValue,
                                    onChanged: _handleRadioValueChange,
                                    activeColor: Colors.blue,

                                  ),
                                  Container(
                                    margin: EdgeInsets.all(10),

                                    child:Text(question_list[position].option_2,
                                      style: TextStyle(fontSize: 18.0,color: Colors.white),),
                                  ),
                                ],
                              ),
                            ],
                          ),
                          ButtonTheme(
                            minWidth: 300.0,
                            child:new RaisedButton(

                              //  padding: const EdgeInsets.fromLTRB(15.0,5.0, 15.0, 5.0),
                                color:  Color(0xFFD0D0DA),
                                elevation: 4.0,
                                disabledColor: Color(0xFFD0D0DA),
                                // splashColor: Colors.blue,
                                child: new Text(position == question_list.length-1 ?"SUBMIT" :"NEXT"
                                  ,style: TextStyle(color: Colors.black,fontSize: 16.0),),
                                onPressed: () {

                                  cat_para = cat_para + "1"+",";
                                  sub_cat_para = sub_cat_para + "1"+",";
                                  ques_id_para = ques_id_para +question_list[position].ques_id.toString()+",";
                                  answer_para = answer_para + _radioValue.toString() + ",";

                                  cat.add(1);
                                  sub_cat.add(1);
                                  ques_id.add(question_list[position].ques_id);
                                  // ques_ans.add(_radioValue);


                                  var t =[1.toString(),1.toString(),question_list[position].ques_id.toString(),_radioValue.toString()];
                                  _final.add(t);

                                  ;


                                  //print(position);
                                  if(position == question_list.length-1){

                                    submit();
                                    print("SUBMitting");


                                  }
                                  else{

                                    _scrollController.animateTo(_scrollController.position.extentInside, duration: new Duration(seconds: 1), curve: Curves.ease);
                                  }




                                },
                                shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                            ),
                          )

                        ],
                      ),
                    );
                  }
              ),

上面的代码可以很好地工作到位置,但是那之后不问问题3的原因?我已经检查了所有事情,例如检查位置和问题总数,一切都很好,但是没有跳到第3个问题。 另外,如果我这样做

_scrollController.position.maxScrollEvent

这将是提交问题的最后一个问题,这表明我已经正确解析了json。请帮我解决这种情况。在此先感谢。

0 个答案:

没有答案