2017-11-25 152 views
1

我必須創建一個心理反應時間測試應用程序。爲此,我必須準確地控制圖像何時可見,並準確測量圖像可見性開始和反應開始(例如點擊事件)之間的延遲。 我該如何在Flutter中實現?具體而言,在同一地點顯示和隱藏多個不同圖像的最有效方式是什麼?我如何才能準確知道點擊事件的發生與真實和完全可見性的發生相關,並考慮到幀設備的速率?是否有辦法對這個過程進行低級控制。通常Flutter似乎暴露了高級別的API。Flutter應用程序中顯示圖像和用戶反應的準確時間

回答

3

我已經做出了嘗試是有可能正是你在找什麼,我的邏輯如下:

  1. 添加監聽到的圖像呈現。
  2. 使用Stopwatch類,我通知我的對象在圖像顯示後開始計時。
  3. 當點擊正確答案時,我停止Stopwatch停止計數。
  4. 保存我目前的分數,並繼續下一個問題。

注:

  • 在這個例子中,爲簡單起見,我沒有爲哪個答案是正確的,這是不是一個帳戶。
  • 我創建了一個新的StatelessWidget來保存每個問題,使用PageView.builder也可能是一個很好的用法。

簡單的例子:

enter image description here

import 'dart:async'; 

import 'package:flutter/material.dart'; 

int score = 0; 

class TimeTest extends StatefulWidget { 
    Widget nextQuestionWidget; //to navigate 
    String question; 
    NetworkImage questionImage; 
    List<String> answers; 

    TimeTest(
     {this.questionImage, this.question, this.answers, this.nextQuestionWidget }); 

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

class _TimeTestState extends State<TimeTest> { 
    final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); 
    bool _loading = true; 
    Stopwatch timer = new Stopwatch(); 

    @override 
    void initState() { 
    widget.questionImage.resolve(new ImageConfiguration()).addListener((_, __) { 
     if (mounted) { 
     setState(() { 
      _loading = false; 
     }); 
     timer.start(); 
     } 
    }); 
    } 

    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     key: _scaffoldKey, 
     appBar: new AppBar(title: new Text("Time Test"),), 
     body: new Container(
     alignment: FractionalOffset.center, 
     margin: const EdgeInsets.symmetric(vertical: 15.0), 
     child: new Column(
      children: <Widget>[ 
      new Text(widget.question), 
      new Divider(height: 15.0, color: Colors.blueAccent,), 
      new CircleAvatar(backgroundImage: widget.questionImage, 
       backgroundColor: Colors.transparent,), 
      new Container(height: 15.0,), 
      new Column(
       children: new List.generate(widget.answers.length, (int index) { 
       return new FlatButton(
        onPressed:() { 
        ///TODO 
        ///add conditions for correct or incorrect answers 
        ///and some manipulation on the score 
        timer.stop(); 
        score = score + timer.elapsedMilliseconds; 
        print(score); 
        _scaffoldKey.currentState.showSnackBar(new SnackBar(
         content: new Text(
          "Your answered this question in ${timer 
           .elapsedMilliseconds}ms"))); 

        ///Hold on before moving to the next question 
        new Future.delayed(const Duration(seconds: 3),() { 
         Navigator.of(context).push(new MaterialPageRoute(
          builder: (_) => widget.nextQuestionWidget)); 
        }); 
        }, child: new Text(widget.answers[index]),); 
       }), 
      ) 
      ], 

     ),), 
    ); 
    } 
} 


class QuestionOne extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new TimeTest(question: "Which animal is in this photo?", 
     questionImage: new NetworkImage(
      "http://cliparting.com/wp-content/uploads/2016/09/Tiger-free-to-use-clipart.png"), 
     answers: ["Lion", "Tiger", "Cheetah"], 
     nextQuestionWidget: new QuestionTwo(),); 
    } 
} 

class QuestionTwo extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new TimeTest(question: "Which bird is in this photo?", 
     questionImage: new NetworkImage(
      "http://www.clker.com/cliparts/P/q/7/9/j/q/eagle-hi.png"), 
     answers: ["Hawk", "Eagle", "Falcon"], 
     nextQuestionWidget: new ResultPage(),); 
    } 
} 

class ResultPage extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     appBar: new AppBar(title: new Text("Result"),), 
     body: new Center(
     child: new Text("CONGRATULATIONS! Your score is $score milliseconds"),), 
    ); 
    } 
} 
void main() { 
    runApp(new MaterialApp(home: new QuestionOne())); 
} 
+0

Grrrrrreat !!!!! – Michael

+0

此解決方案在屏幕上推送新路線。是不是像大量路線的內存泄漏?我寧願替換現有的圖像我如何存檔? – Michael

+0

我不想使用pushReplacement,因爲它取代了整個小部件。是否可以簡單地只交換圖像並在顯示時準確通知? – Michael

相關問題