Dart跨页持久化数据



对于一个智力竞赛应用程序,我想给各种变量打分。最后,应该显示具有最多点的变量。

测验共有6页,每个问题都可以回答是或否。我已经尝试为每个CCD_ 1页设置变量;var教师=0;并且在Flatbutton下具有++(例如变量teacher ++(以使变量向上计数。

这在第一页运行得很好,但在第二页,它在0开始再次计数。

你有关于如何最好地编程以及如何全面设置可变页面的提示吗?

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),),
],
)
]
),
)
);
}
}

当您更改页面调用navigator.push时,当前状态将被破坏。要在屏幕上保持数据,您可以使用不同的方法:

  • 使用SQLite
  • 使用共享首选项
  • 使用文件

我给你这个链接,了解如何做这些事情的更多细节:https://flutter.dev/docs/cookbook/persistence

最新更新