简体   繁体   中英

Dart persist data across pages

For a quiz app, I want to award points to various variables. At the end, the variable with the most points should be displayed.

The quiz has a total of 6 pages and each question can be answered with yes or no. I have already tried to set the variables for each StatefulWidget page "var teacher = 0; and under Flatbutton with ++ eg the variable teacher ++ , to let the variable count up.

This works quite well for the first page, but on the second page it starts counting again at 0.

Do you have a tip on how to best program this and how to set variable pages across the board?

class Frage1 extends StatefulWidget {
     @override
       _Frage1State createState() => _Frage1State();
     }


class _Frage1State extends State<Frage1> {

    String _ergebnis = "0";

    var teacher = 0;
    var waiter = 0;
    var bus_driver = 0;
    var gardener = 0;

    String Yes_No = '';


    @override
    Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          shape: Border.all(width: 1 ),
          elevation: 10,
          centerTitle: true,
          title: Text('AMF', style: TextStyle(fontFamily: 'mezzanine.ttf',
              fontWeight: FontWeight.w500,
              fontSize: 65,
              color: Colors.red[700],
              letterSpacing: 15),),
      ),backgroundColor: Colors.black,
            body: Column(children: <Widget>[
              Padding(padding: EdgeInsets.symmetric(horizontal: 10, vertical: 80),),
              Text('1. Do you have fun to work with other people?'
            , style: TextStyle(color: Colors.red[700], fontFamily: 'mezzanine.ttf',fontSize: 38),
                textAlign: TextAlign.center,
            ),
            Row(children: [
              FlatButton(onPressed: (){
                setState(() {
                 
                  teacher++; 
                  waiter++;
                  bus_driver++;

        

                  print(teacher);
                  print(waiter);
                  print(bus_driver);

                });
              debugPrint('Button clicked');

              Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage2()));},

                 child: Text('Yes'
                   ,style: TextStyle(color: Colors.red[700],
                       fontSize: 38.00,
                       fontFamily: 'mezzanine.ttf'
                   ),
                   textAlign: TextAlign.center,
                 ),

              padding: EdgeInsets.symmetric(horizontal: 82, vertical: 90),),
               FlatButton(onPressed: (){
                 setState(() {

                   gardener++;

                   print(gardener);

                 });
              debugPrint('Button clicked');
              Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage2()));},
                child: Text('No'
                  ,style: TextStyle(color: Colors.red[700],
                      fontSize: 38.00,
                      fontFamily: 'mezzanine.ttf'
                  ),
                  textAlign: TextAlign.center,
                ),
                 padding: EdgeInsets.symmetric(horizontal: 40, vertical: 90),),
            ],
            )
            ]
            ),
          )
        );
        }
       @override
       void debugFillProperties(DiagnosticPropertiesBuilder properties) {
        super.debugFillProperties(properties);
        properties.add(DiagnosticsProperty('int', int));
       }
        }

      class Frage2 extends StatefulWidget {
      @override
       _Frage2State createState() => _Frage2State();
        }

       class _Frage2State extends State<Frage2> {

       String _ergebnis = "0";


       var teacher = 0; 
       var waiter = 0;
       var bus_driver = 0;
       var gardener = 0; 

       String Yes_No = ''; 

       @override
       Widget build(BuildContext context) {
        return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black,
            shape: Border.all(width: 1 ),
            elevation: 10,
            centerTitle: true,
            title: Text('AMF', style: TextStyle(fontFamily: 'mezzanine.ttf',
                fontWeight: FontWeight.w500,
                fontSize: 65,
                color: Colors.red[700],
                letterSpacing: 15),),
          ),backgroundColor: Colors.black,
          body: Column(children: <Widget>[
            Padding(padding: EdgeInsets.symmetric(horizontal: 10, vertical: 90),),
            Text('2. do you like it to drive ?'
              , style: TextStyle(color: Colors.red[700], fontFamily: 'mezzanine.ttf',fontSize: 38),
              textAlign: TextAlign.center,
            ),
            Row(children: [FlatButton(onPressed: (){
              setState(() {
               
              bus_driver++;

              print(bus_driver);

              });
              debugPrint('Button clicked');
              Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage3()));},

              child: Text('Yes'
                ,style: TextStyle(color: Colors.red[700],
                    fontSize: 38.00,
                    fontFamily: 'mezzanine.ttf'
                ),
                textAlign: TextAlign.center,
              ),
              padding: EdgeInsets.symmetric(horizontal: 82, vertical: 90),),
              FlatButton(onPressed: (){
                  setState(() {

                 
                  teacher++;
                  waiter++;
                  gardener++; 

                  print(teacher);
                  print(waiter);
                  print(gardener);
                 
                  });

                debugPrint('Button clicked');
                Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage3()));},
                child: Text('No'
                  ,style: TextStyle(color: Colors.red[700],
                      fontSize: 38.00,
                      fontFamily: 'mezzanine.ttf'
                  ),
                  textAlign: TextAlign.center,
                ),
                padding: EdgeInsets.symmetric(horizontal: 40, vertical: 90),),
            ],
            )
          ]
          ),
        )
    );
  }
}

When you change page calling navigator.push the current state is destroyed. To persist data across screen you can use different methods:

  • use SQLite
  • use Shared preferences
  • use a file

I give you this link for further details on how to do these things: https://flutter.dev/docs/cookbook/persistence

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM