使用 flutter stateful widget 解析数据并使用它
parssing data with flutter stateful widget and using it
我想在覆盖之前使用通过 if 语句解析的数据,
怎么做?
我尝试了很多方法但没有奏效。
请帮助我。
import 'package:flutter/material.dart';
class MyButton extends StatefulWidget {
Function buttonFunction;
String buttonName;
MyButton(this.buttonFunction,this.buttonName);
@override
_MyButtonState createState() => _MyButtonState();
}
class _MyButtonState extends State<MyButton> {
if (widget.buttonName == "ok"){
int i =1;
}
else int i =2;
@override
Widget build(BuildContext context) {
return Container(
child: RaisedButton(
onPressed: () {
widget.buttonFunction();
},
child: Text("${widget.buttonName}",
),
),
);
}
}```
您应该在 initState
方法中进行大部分初始化。
int i;
@override
void initState() {
if (widget.buttonName == "ok") {
i = 1;
} else
i = 2;
super.initState();
}
我想在覆盖之前使用通过 if 语句解析的数据, 怎么做? 我尝试了很多方法但没有奏效。 请帮助我。
import 'package:flutter/material.dart';
class MyButton extends StatefulWidget {
Function buttonFunction;
String buttonName;
MyButton(this.buttonFunction,this.buttonName);
@override
_MyButtonState createState() => _MyButtonState();
}
class _MyButtonState extends State<MyButton> {
if (widget.buttonName == "ok"){
int i =1;
}
else int i =2;
@override
Widget build(BuildContext context) {
return Container(
child: RaisedButton(
onPressed: () {
widget.buttonFunction();
},
child: Text("${widget.buttonName}",
),
),
);
}
}```
您应该在 initState
方法中进行大部分初始化。
int i;
@override
void initState() {
if (widget.buttonName == "ok") {
i = 1;
} else
i = 2;
super.initState();
}