flutter 全局变量或将变量值传递到另一个屏幕

nbewdwxp  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(175)

我想这个问题很愚蠢,但我一天也解不出这个最简单的问题。
在类“MyHomePage”中,我有一个变量“bal”,用于计算正确答案。

import 'package:australia/qlist.dart';
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 1;
  int bal = 0;
  int bad = 0;


  List<Color> colorsList = [
    Colors.white,
    Colors.white,
    Colors.white,
    Colors.white
  ];

  List<String> answers = [];
  @override
  void initState() {
    answers.addAll([
      QuestionsList.shared.getCurrentQuestion().correctAnswer,
      QuestionsList.shared.getCurrentQuestion().dis1,
      QuestionsList.shared.getCurrentQuestion().dis2,
      QuestionsList.shared.getCurrentQuestion().dis3,
    ]);
    answers.shuffle();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text ("Exam test"),
        ),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [

              Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      children: [
                        Text('$_counter'), Text('/20____'), Text('$bal'), Text('____'), Text('$bad'),
                      ],
                    ),
                  ),

                  Center(
                      child: Text(QuestionsList.shared.getCurrentQuestion().question)),
                ],
              ),

              Column(
                children: [
                  Container(
                    width: double.infinity,
                    child: Card(
                      color: colorsList[0],
                      child: InkWell(
                        child: Container(
                            margin: const EdgeInsets.all(5.0),
                            child: Text(answers[0])),
                        onTap: () {
                          if (answers[0] ==
                              QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                            colorsList[0] = Colors.green;
                            setState(() {
                              if (bad==0) {bal++;}
                              ;
                            });
                          } else {
                            colorsList[0] = Colors.red;
                            setState(() {bad++;});
                          }
                        },
                      ),
                    ),),
                  Container(
                    width: double.infinity,
                    child: Card(
                      color: colorsList[1],
                      child: InkWell(
                        child: Container(
                            margin: const EdgeInsets.all(5.0),
                            child: Text(answers[1])),
                        onTap: () {
                          if (answers[1] ==
                              QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                            colorsList[1] = Colors.green;
                            setState(() {
                              if (bad==0) {bal++;};
                            });
                          } else {
                            colorsList[1] = Colors.red;
                            setState(() {bad++;});
                          }
                        },
                      ),
                    ),),
                  Container(
                    width: double.infinity,
                    child: Card(
                      color: colorsList[2],
                      child: InkWell(
                        child: Container(
                            margin: const EdgeInsets.all(5.0),
                            child: Text(answers[2])),
                        onTap: () {
                          if (answers[2] ==
                              QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                            colorsList[2] = Colors.green;
                            setState(() {
                              if (bad==0) {bal++;};
                            });
                          } else {
                            colorsList[2] = Colors.red;
                            setState(() {bad++;});
                          }
                        },
                      ),
                    ),),
                  Container(
                    width: double.infinity,
                    child: Card(
                      color: colorsList[3],
                      child: InkWell(
                        child: Container(
                            margin: const EdgeInsets.all(5.0),
                            child: Text(answers[3])),
                        onTap: () {
                          if (answers[3] ==
                              QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                            colorsList[3] = Colors.green;
                            setState(() {
                              if (bad==0) {bal++;};
                            });
                          } else {
                            colorsList[3] = Colors.red;
                            setState(() {bad++;});
                          }
                        },
                      ),
                    ),),


                  ElevatedButton(
                      onPressed: () {
                        QuestionsList.shared.nextQuestion();
                        colorsList[0] = Colors.white;
                        colorsList[1] = Colors.white;
                        colorsList[2] = Colors.white;
                        colorsList[3] = Colors.white;
                        answers.clear();
                        answers.addAll([
                          QuestionsList.shared.getCurrentQuestion().correctAnswer,
                          QuestionsList.shared.getCurrentQuestion().dis1,
                          QuestionsList.shared.getCurrentQuestion().dis2,
                          QuestionsList.shared.getCurrentQuestion().dis3,
                        ]);
                        answers.shuffle();
                        setState(() {
                          _counter++; bad=0;
                          if (_counter==20) {Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context)
                                => PageResult()

                            ),
                          );}
                        });
                      },
                      child: Text('NEXT')),
                  ElevatedButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      child: Text('BACK')),

                ],
              )
            ]));
  }
}

我需要把它的值传递给“PageResult”类。

class PageResult extends StatefulWidget {
  
  createState() => new MyWidgetState();
}
class MyWidgetState extends State<PageResult> {

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text ("Test Result")
    ),
// body: Text('$bal'),


    );
  }
}

我知道有两种解决方案。
1.在类之间传递数据
2.使“bal”变量成为全局变量。
但我在手册上找不到解决办法。请帮帮忙。

n6lpvg4x

n6lpvg4x1#

可以用PageResult小部件的参数化构造函数传递变量,并与State中的引用widget.bal一起使用

class PageResult extends StatefulWidget {
  PageResult({required this.bal}) : super() ;
  final int bal;  

  createState() => new MyWidgetState();
}
class MyWidgetState extends State<PageResult> {

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text ("Test Result")
    ),
body: Center(
        child: Text('${widget.bal}'),
      )
    );
  }
}

在推进路线的同时

Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context)
                                => PageResult(bal: bal)

                            ),
                          );

相关问题