如何在 Flutter 中增加计数器并禁用按钮?
How do I increment a counter and disable a button in Flutter?
我有一个按钮,我想在增加计数器后禁用它。这是我用来增加计数器的方法:
void incrementAdCounter() async {
setState(() {
adCounter++;
if (adCounter == 2 || adCounter > 2) {
isAdButtonDisabled = true;
}
setAdCounter();
print(adCounter);
});
}
在 initState 内部:
void initButtons() {
isAdButtonDisabled = false;
adCounter = 0
}
我有一个按钮,我在 onTap 上调用了这些方法,如果按钮:
void getCounters() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
adCounter = prefs.getInt('adCounter');
isAdButtonDisabled = prefs.getBool('isAdButtonDisabled');
}
setAdCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('adCounter', adCounter);
prefs.setBool('isAdButtonDisabled', isAdButtonDisabled);
}
当我调用此方法时,它显示以下错误:
Unhandled Exception: NoSuchMethodError: The method '+' was called on null.
E/flutter (13166): Receiver: null
int adCounter=0;(随便你先初始化值)
void incrementAdCounter() async {
setState(() {
adCounter++;
if (adCounter == 2 || adCounter > 2) {
isAdButtonDisabled = true;
}
setAdCounter();
print(adCounter);
});
}
对原始代码的这种修改将完全满足您的要求:
int adCounter = 0;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(adCounter.toString()),
RaisedButton(
onPressed: adCounter >= 2 ? null : incrementAdCounter,
child: Text('Increment'),
),
],
)
);
}
void incrementAdCounter() {
setState(() {
adCounter++;
});
}
您的其余代码不需要修改,但如果您的按钮被禁用则您不需要保存到 SharedPreferences。您可以根据值对其进行验证。
试试这个,
void getCounters() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
adCounter = prefs.getInt('adCounter') ?? 0;
isAdButtonDisabled = prefs.getBool('isAdButtonDisabled');
}
我有一个按钮,我想在增加计数器后禁用它。这是我用来增加计数器的方法:
void incrementAdCounter() async {
setState(() {
adCounter++;
if (adCounter == 2 || adCounter > 2) {
isAdButtonDisabled = true;
}
setAdCounter();
print(adCounter);
});
}
在 initState 内部:
void initButtons() {
isAdButtonDisabled = false;
adCounter = 0
}
我有一个按钮,我在 onTap 上调用了这些方法,如果按钮:
void getCounters() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
adCounter = prefs.getInt('adCounter');
isAdButtonDisabled = prefs.getBool('isAdButtonDisabled');
}
setAdCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('adCounter', adCounter);
prefs.setBool('isAdButtonDisabled', isAdButtonDisabled);
}
当我调用此方法时,它显示以下错误:
Unhandled Exception: NoSuchMethodError: The method '+' was called on null.
E/flutter (13166): Receiver: null
int adCounter=0;(随便你先初始化值)
void incrementAdCounter() async {
setState(() {
adCounter++;
if (adCounter == 2 || adCounter > 2) {
isAdButtonDisabled = true;
}
setAdCounter();
print(adCounter);
});
}
对原始代码的这种修改将完全满足您的要求:
int adCounter = 0;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(adCounter.toString()),
RaisedButton(
onPressed: adCounter >= 2 ? null : incrementAdCounter,
child: Text('Increment'),
),
],
)
);
}
void incrementAdCounter() {
setState(() {
adCounter++;
});
}
您的其余代码不需要修改,但如果您的按钮被禁用则您不需要保存到 SharedPreferences。您可以根据值对其进行验证。
试试这个,
void getCounters() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
adCounter = prefs.getInt('adCounter') ?? 0;
isAdButtonDisabled = prefs.getBool('isAdButtonDisabled');
}