我正在尝试访问另一个类的方法,因为我将类从 StatelessWidget
更改为 StatefulWidget
我无法访问 StatefulWidget
中的 addItemToList 方法,我想使用 setState,虽然我知道它只适用于 StatefulWidget
或 StatelessWidget
。
这是我正在使用的代码,第一部分是我试图调用 addItemToList
的方法所在的位置:
class ScoreScreen extends StatefulWidget {
@override
_ScoreScreenState createState() => _ScoreScreenState();
}
class _ScoreScreenState extends State<ScoreScreen> {
String _nome;
String get nome => this._nome;
QuestionController _qnController = Get.put(QuestionController());
final List<String> names = <String>[
'Aby',
'Aish',
'Ayan',
'Ben',
'Bob',
'Charlie',
'Cook',
'Carline'
];
final List<int> msgCount = <int>[2, 0, 10, 6, 52, 4, 0, 2];
List<int> auxInt = <int>[];
List<String> auxString = <String>[];
void addItemToList() async {
setState(() {
names.insert(0, _qnController.nome);
msgCount.insert(0, _qnController.numOfCorrectAns);
auxInt = msgCount;
auxString = names;
print(names);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(....
第二部分是我尝试调用该方法的地方,只是为了记住它与 StatelessWidget
一起工作,但现在我进行了更改,它不是并说 The method 'addItemToList' isn't defined for the type 'ScoreScreen'.
;
// We use get package for our state management
ScoreScreen score = Get.put(ScoreScreen());
class QuestionController extends GetxController
with SingleGetTickerProviderMixin {
// Lets animated our progress bar
AnimationController _animationController;
Animation _animation;
// so that we can access our animation outside
Animation get animation => this._animation;
PageController _pageController;
PageController get pageController => this._pageController;
List<Question> _questions = sample_data.map((question) {
// shuffle the options
List<String> _options = question["options"];
//_options.shuffle();
return Question(
id: question['id'],
question: question['question'],
options: _options,
answer: question['answer'],
bandeira: question['bandeira']);
}).toList()
..shuffle(); // remove this shuffle to check if options are shuffled
List<Question> get questions => this._questions;
bool _isAnswered = false;
bool get isAnswered => this._isAnswered;
String _correctAns;
String get correctAns => this._correctAns;
String _selectedAns;
String get selectedAns => this._selectedAns;
int _indexQ;
int get indexQ => this._indexQ;
// for more about obs please check documentation
RxInt _questionNumber = 1.obs;
RxInt get questionNumber => this._questionNumber;
String _nome;
String get nome => this._nome;
int _numOfCorrectAns = 0;
int get numOfCorrectAns => this._numOfCorrectAns;
// called immediately after the widget is allocated memory
@override
void onInit() {
// Our animation duration is 60 s
// so our plan is to fill the progress bar within 60s
_animationController =
AnimationController(duration: Duration(seconds: 60), vsync: this);
_animation = Tween<double>(begin: 0, end: 1).animate(_animationController)
..addListener(() {
// update like setState
update();
});
// start our animation
// Once 30s is completed go to the next qn
_animationController.forward().whenComplete(nextQuestion);
_pageController = PageController();
super.onInit();
if (_questionNumber == _questions.length) {
reset();
Get.to(() => WelcomeScreen());
_animationController.reset();
}
}
// // called just before the Controller is deleted from memory
@override
void onClose() {
super.onClose();
_animationController.dispose();
_pageController.dispose();
}
void checkAns(Question question, int selectedIndex, List opcoes) {
// because once user press any option then it will run
_isAnswered = true;
_correctAns = question.answer;
_selectedAns = opcoes[selectedIndex];
int index = opcoes.indexOf(_correctAns);
if (selectedIndex == index) _numOfCorrectAns++;
// It will stop the counter
_animationController.stop();
update();
// Once user select an ans after 2s it will go to the next qn
Future.delayed(Duration(seconds: 1), () {
nextQuestion();
});
}
void nextQuestion() {
if (_questionNumber.value != _questions.length) {
_isAnswered = false;
_pageController.nextPage(
duration: Duration(milliseconds: 250), curve: Curves.ease);
// Reset the counter
_animationController.reset();
// Then start it again
// Once timer is finish go to the next qn
_animationController.forward().whenComplete(nextQuestion);
} else {
// Get package provide us simple way to naviigate another page
// HERE I TRY TO CALL THE METHOD, but it is not working
ScoreScreen().addItemToList();
Get.to(() => ScoreScreen());
}
}