增加奖励点数

我想制作一个奖励积分应用程序,因为我使用了随机函数。

问题是我不知道如何将此号码添加到先前的号码。得到总分。

增加奖励点数

这是我想要在此处求和的结果。

增加奖励点数

修改后,我收到此错误

增加奖励点数

这是我的奖励应用程序的完整代码

rewards.dart

import 'dart:async';
import 'package:fid786/Services/CardScreen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fid786/styles.dart';
import 'package:fid786/Services/Clasnumber.dart';
class RewardsScreen extends StatefulWidget {
@override
_RewardsScreenState createState() => _RewardsScreenState();
}
class _RewardsScreenState extends State<RewardsScreen> {
@override
void initState() {
super.initState();
startTimer();
}
startTimer() async{
  var duration=Duration(seconds: 4);
  return Timer(duration,route);
}
route(){
  Navigator.pushReplacement(context,MaterialPageRoute(builder: 
(context)=>CardScreen()));
}                       
@override
Widget build(BuildContext context) {
return Scaffold(
  resizeToAvoidBottomPadding: false,body: AnnotatedRegion<SystemUiOverlayStyle>(
    value: SystemUiOverlayStyle.light,child: Container(
    height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,end: Alignment.bottomCenter,stops: [0.1,0.4,0.7,0.9],colors: [
            Color(0xFF3594DD),Color(0xFF4563DB),Color(0xFF5036D5),Color(0xFF5B16D0),],),child: Padding(
        padding: EdgeInsets.only(top:80),child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,children: <Widget>[
             Expanded(
                    flex: 8,child:Container(
              height: MediaQuery.of(context).size.height,//height: 600.0,child: PageView(
                children: <Widget>[
                 
                    Padding(
                     padding: EdgeInsets.symmetric(horizontal: 2),child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
                        Center(
                          child: ClipRRect(
                          borderRadius: BorderRadius.circular(50.0),child: Image(
                            image: AssetImage(
                              'assets/images/congrats.gif',height: 300.0,width: 300.0,fit: BoxFit.fill,SizedBox(height: 30.0),Text(
                          ' Congratulations,You have Got',style: kTitleStyle,SizedBox(height: 20.0),Center(
                        child:Text(
                          Classname.generateRandomNumber().toString(),style: TextStyle(color: Colors.greenaccent,fontWeight: FontWeight.bold,fontSize: 50)),SizedBox(height: 10.0),Center(
                        child:Text(
                          '  Points ',SizedBox(height: 50.0),Center(
                        child:Text(
                          'Earn More Points and Win Prizes ',style:
                          TextStyle(
                          color: Colors.yellowaccent,fontSize: 16,fontFamily: 'muso',decoration: TextDecoration.underline),);
}
}

文件CardScreen.dart

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:fid786/ScanQR/Card_Scanner.dart';
import 'package:fid786/Authentification/LogInScreen.dart';
import 'package:fid786/Services/Clasnumber.dart';
class CardScreen extends StatefulWidget {
@override
_CardScreenState createState()=>_CardScreenState();

}
class _CardScreenState extends State<CardScreen>{
FirebaseAuth auth=FirebaseAuth.instance;
Future<void>logOut() async{
FirebaseUser user=auth.signOut() as FirebaseUser; 
return user; 
}
@override
void initState() {
super.initState();
}
int points = 0;
int generateNumber() {
                       
                      int newPoints = Classname.generateRandomNumber();
setState(() {
points += newPoints;
});
return points;
                        }
@override
Widget build(BuildContext context){

return Scaffold(
  backgroundColor:Color(0xffffffff),appBar:AppBar(
    backgroundColor: Colors.greenaccent,title:Text("Loyalty Points"),actions: <Widget>[
      SizedBox(width:1.0),flatButton.icon(onpressed: (){
        logOut();
        Navigator.pushReplacement(context,MaterialPageRoute(builder: 
   (BuildContext context)=>LogInScreen()));
      },icon: Icon(Icons.shopping_cart,size: 40.0,color: 
   Colors.pinkaccent,label: Text("Log Out",style: TextStyle(color: 
   Colors.pinkaccent),))
   ],body: StreamBuilder(
      stream:Firestore.instance.collection('screen').snapshots(),builder:(context,snapshot){
          if(!snapshot.hasData) return Text('');
          
    return Column(
    children: <Widget>[
        Padding(
          
                    padding: EdgeInsets.only(left: 8.0,right: 8.0,top: 
    40.0),child: Container(
                      width: MediaQuery.of(context).size.width,height: 80.0,child: Padding(
                        padding: EdgeInsets.only(top: 8.0,bottom: 8.0),child: Material(
                          shape: Border.all(width: 2.0,color: 
    Colors.blueaccent),color: Colors.white,elevation: 18.0,shadowColor: Color(0x802196F3),child: Center(
                            child: Padding(
                              padding: EdgeInsets.all(5.0),child: Row(
                               // mainAxisAlignment:
                                 //   MainAxisAlignment.spaceBetween,children: <Widget>[       
                                  Text(
                                    snapshot.data.documents[0]['player'],style: TextStyle(
                                        color: Colors.greenaccent,fontSize: 18.0),// mainAxisAlignment:
                                 //   MainAxisAlignment.spaceBetween,SizedBox(width:175),Column(
                                    
                                    mainAxisAlignment:
                                        MainAxisAlignment.center,children: <Widget>[
                                         
                        Text(
                          generateNumber().toString(),fontSize: 25)),Text(
                                        'Points',style: TextStyle(
                                            color: Colors.pinkaccent,fontSize: 16.0,fontFamily: 'muso'),)
                                ],);
      }
    ),floatingactionButton: FloatingactionButton(
    backgroundColor: Color(0xFFFA7397),child: Icon(
      FontAwesomeIcons.listUl,color: Color(0xFFFDDE42),onpressed: () {
      Navigator.push(
        context,MaterialPageRoute(
            builder: (context) => HomePage(),fullscreenDialog: true),);
    },)
);
}
}

文件Clasnumber.dart

import 'dart:math';

class Classname{

static int generateRandomNumber() {
List<int> pointvalue = [5,6,7,8,10,15];
return pointvalue[new Random().nextInt(pointvalue.length)];
}
}
iCMS 回答:增加奖励点数

您可以使用setState()函数来实现此目的。

int points = 0;
int newPoints = generateRandomNumber();
setState(() {
  points += newPoints
});

如果要全局存储总积分值,则应查看shared preferences包。

能够全局使用该函数的最佳方法是将其包装在单独的全局类中。

class ClassName{
  static int generateRandomNumber() {
    return pointValue[new Random().nextInt(pointValue.length)];
  }
}

然后在其他课程中,您可以这样调用:

ClassName.generateRandomNumber();

您得到的错误是由于在build()函数外部调用setState引起的。像这样将生成编号函数移到构建函数中

@override
Widget build(BuildContext context){
int generateNumber() {
 int newPoints = ClassName.generateRandomNumber();
 setState(() {
   points += newPoints;
 });
 return points;
}


本文链接:https://www.f2er.com/2024879.html

大家都在问