2017-05-12 2 views
0

나는이 플러터 코드 (퀴즈 용)를 가지고 있으며 제출 버튼을 다음 질문으로 이동하고 싶습니다. 그러나 submitQuestion()을 사용하면 버튼이 활성화되지 않습니다. 아니 애니메이션 포함제출 버튼을 사용하여 질문 목록을 단계별 실행하는 방법

import 'dart:async'; 
import 'package:flutter/material.dart'; 
import 'dart:io'; 

var _counter = 0; 
var bkgrdColor = Colors.blue[50]; 

bool isAnswerCorrect1 = (questions[_counter].answers[_ansCounter1] == 
    questions[_counter].correctAnswer); 
bool isAnswerCorrect2 = (questions[_counter].answers[_ansCounter2] == 
    questions[_counter].correctAnswer); 
bool isAnswerCorrect3 = (questions[_counter].answers[_ansCounter3] == 
    questions[_counter].correctAnswer); 
bool isAnswerCorrect4 = (questions[_counter].answers[_ansCounter4] == 
    questions[_counter].correctAnswer); 

final int _ansCounter1 = 0; 
final int _ansCounter2 = 1; 
final int _ansCounter3 = 2; 
final int _ansCounter4 = 3; 

final int ans1Value = 1; 
final int ans2Value = 2; 
final int ans3Value = 3; 
final int ans4Value = 4; 

var TIMEOUT = const Duration(seconds: 3); 
var ms = const Duration(milliseconds: 1); 

List<Question> questions = [ 
    new Question("What is 2+2", ["2", "3", "4", "5"], "4"), 
    new Question("What is 1+1", ["2", "3", "4", "5"], "2"), 
    new Question("What is 4+4", ["7", "8", "10", "16"], "8"), 
    new Question("What is 5+5", ["10", "12", "25", "20"], "10"), 
    new Question("What is 3+3", ["4", "5", "6", "7"], "6") 
]; 

class Question { 
    String question; 
    List<String> answers; 
    String correctAnswer; 
    Question(this.question, this.answers, this.correctAnswer); 
} 

void main() { 
    runApp(new _questionDisplay()); 
} 

class _questionDisplay extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new MaterialApp(home: new QuestDis()); 
    } 
} 

class QuestDis extends StatefulWidget { 
    QuestDis({Key key}) : super(key: key); 

    @override 
    _QuestDisState createState() => new _QuestDisState(); 
} 

class _QuestDisState extends State<QuestDis> with TickerProviderStateMixin { 
    AnimationController _controller; 
    int radioValue = 0; 

    @override 
    initState() { 
    _controller = new AnimationController(
     vsync: this, 
     duration: const Duration(milliseconds: 100), 
    )..addStatusListener((AnimationStatus status) { 
     if (status == AnimationStatus.completed) _controller.reverse(); 
     }); 
    super.initState(); 
    } 

    void handleRadioValueChanged(int value) { 
    if (radioValue != 0) _controller.forward(); 
    setState(() { 
     radioValue = value; 
     print("Radio value is: $radioValue"); 
     switch (radioValue) { 
     case 1: 
      bkgrdColor = isAnswerCorrect1 ? Colors.green[100] : Colors.red[100]; 
      break; 
     case 2: 
      bkgrdColor = isAnswerCorrect2 ? Colors.green[100] : Colors.red[100]; 
      break; 
     case 3: 
      bkgrdColor = isAnswerCorrect3 ? Colors.green[100] : Colors.red[100]; 
      break; 
     case 4: 
      bkgrdColor = isAnswerCorrect4 ? Colors.green[100] : Colors.red[100]; 
      break; 
     } 
     submitButton(); 
    }); 
    } 

    submitButton() { 
    _counter++; 
    radioValue = 0; 
    bkgrdColor = Colors.blue[50]; 
    } //nextQuestion() } 

    Widget build(BuildContext context) { 
    ThemeData theme = Theme.of(context); 
    var container = new Container(
     child: new Column(
     children: [ 
      new Column(
      children: [ 
       new Row(
       children: [ 
        new Expanded(
        child: new Container(
         child: new Row(
         children: [ 
          new Radio<int>(
           value: ans1Value, 
           groupValue: radioValue, 
           onChanged: handleRadioValueChanged), 
          new Text(
           "${questions[_counter].answers[_ansCounter1]}") 
         ], 
        ), 
        ), 
       ), 
        new Expanded(
        child: new Container(
         child: new Row(children: [ 
         new Radio<int>(
          value: ans2Value, 
          groupValue: radioValue, 
          onChanged: handleRadioValueChanged), 
         new Text("${questions[_counter].answers[_ansCounter2]}") 
         ]), 
        ), 
       ), 
       ], 
      ), 
      ], 
     ), 
      new Column(
      children: [ 
       new Column(
       children: [ 
        new Row(
        children: [ 
         new Expanded(
         child: new Container(
          child: new Row(
          children: [ 
           new Radio<int>(
            value: ans3Value, 
            groupValue: radioValue, 
            onChanged: handleRadioValueChanged), 
           new Text(
            "${questions[_counter].answers[_ansCounter3]}") 
          ], 
         ), 
         ), 
        ), 
         new Expanded(
         child: new Container(
          child: new Row(children: [ 
          new Radio<int>(
           value: ans4Value, 
           groupValue: radioValue, 
           onChanged: handleRadioValueChanged), 
          new Text(
           "${questions[_counter].answers[_ansCounter4]}") 
          ]), 
         ), 
        ), 
        ], 
       ), 
       ], 
      ), 
      ], 
     ), 
     ], 
    ), 
    ); 

    return new Scaffold(
     appBar: new AppBar(
     leading: new IconButton(icon: new Icon(Icons.menu), onPressed: null), 
     title: new Text('getdata'), 
    ), 
     body: new Container(
     child: new Column(
      children: [ 
      new Expanded(
       //child: new Container(
       child: new Column(
       children: <Widget>[ 
        new Expanded(
        flex: 3, 
        child: new Container(
         child: new Card(
         color: bkgrdColor, 
         child: new Row(
          children: <Widget>[ 
          new Text("${questions[_counter].question}"), 
          ], 
         ), 
        ), 
        ), 
       ), 
        new Expanded(
        flex: 1, 
        child: new Center(
         child: container, 
        ), 
       ), 
        new AnimatedBuilder(
         child: const Text('SUBMIT'), 
         animation: _controller, 
         builder: (BuildContext context, Widget child) { 
         return new RaisedButton(
          color: new ColorTween(
           begin: theme.primaryColor, 
           end: theme.disabledColor, 
          ) 
           .animate(_controller) 
           .value, 
          colorBrightness: Brightness.dark, 
          child: child, 
          onPressed: 
           radioValue == 0 ? null : submitButton()); 
         }) 
       ], 
      ), 
      ), 
      ], 
     ), 
    ), 
    ); 
    } 

    @override 
    void dispose() { 
    _controller.dispose(); 
    super.dispose(); 
    } 
} 

내 접근 방식이 잘못 되었습니까/어떻게 해결할 수 있습니까?

답변

1

onPressed 인수에 submitButton()submitButton으로 대체해야합니다. onPressednull (submitButton을 호출 한 결과) 대신 VoidCallback이되도록하려고합니다.

또한 submitButton 메서드를 수정하여 setState (예 : setState)을 호출합니다. 그렇지 않으면 떨림 당신을 다시 알 수 없습니다 때문에

setState(() { 
    _counter++; 
    radioValue = 0; 
    bkgrdColor = Colors.blue[50]; 
}); 

당신은 당신이 당신의 State의 회원을 수정할 때 항상 setState()를 호출해야합니다.