将状态设置为 onpressed floatingactionbutton 未执行任何更改
Setstate into onpressed floatingactionbutton is not performing any changes
以下代码未更新计数器的状态。
如果我选择使用一个名为 _incremetcounter 的函数,按下 Floatingactionbutton 它就可以工作。
如果我将函数的内容直接放入“按下”动作中,它就不起作用。
请解释一下为什么?下面还有class的代码。
问候。
class _MyHomePageState extends State<MyHomePage> {
int counter = 0;
final appbarcounter = Counter(); //instance creation
void initState() {
super.initState();
appbarcounter.valuetoadd = 5;
}
void _incrementCounter() {
setState(() {
appbarcounter.incrementof();
counter = appbarcounter.countervalue;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
//onPressed: _incrementCounter,
onPressed: () {
setstate() {
appbarcounter.incrementof();
counter = appbarcounter.countervalue;
}
;
},
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class Counter {
Counter() {
this.countervalue;
this.valuetoadd;
countervalue = 0;
valuetoadd = 2;
}
int countervalue; //definisco la variabile contatore
int valuetoadd; //definisco la variabile contatore
int get number {
return valuetoadd;
}
void increment() {
countervalue++;
}
void incrementof() {
countervalue = countervalue + valuetoadd;
}
void decrement() {
countervalue--;
}
void decrementof(int valuetoadd) {
countervalue = countervalue - valuetoadd;
}
}
将 setstate
更改为 setState
。
use this syntax
onPressed: () {
setState(() {
appbarcounter.incrementof();
counter = appbarcounter.countervalue;
});
},
以下代码未更新计数器的状态。 如果我选择使用一个名为 _incremetcounter 的函数,按下 Floatingactionbutton 它就可以工作。 如果我将函数的内容直接放入“按下”动作中,它就不起作用。 请解释一下为什么?下面还有class的代码。 问候。
class _MyHomePageState extends State<MyHomePage> {
int counter = 0;
final appbarcounter = Counter(); //instance creation
void initState() {
super.initState();
appbarcounter.valuetoadd = 5;
}
void _incrementCounter() {
setState(() {
appbarcounter.incrementof();
counter = appbarcounter.countervalue;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
//onPressed: _incrementCounter,
onPressed: () {
setstate() {
appbarcounter.incrementof();
counter = appbarcounter.countervalue;
}
;
},
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class Counter {
Counter() {
this.countervalue;
this.valuetoadd;
countervalue = 0;
valuetoadd = 2;
}
int countervalue; //definisco la variabile contatore
int valuetoadd; //definisco la variabile contatore
int get number {
return valuetoadd;
}
void increment() {
countervalue++;
}
void incrementof() {
countervalue = countervalue + valuetoadd;
}
void decrement() {
countervalue--;
}
void decrementof(int valuetoadd) {
countervalue = countervalue - valuetoadd;
}
}
将 setstate
更改为 setState
。
use this syntax
onPressed: () {
setState(() {
appbarcounter.incrementof();
counter = appbarcounter.countervalue;
});
},