Flutter 颤振显示数据并单击进行更改
我有一个赋值,我需要显示来自API的值,然后按“启动”按钮,然后显示下一个值 这是我的密码Flutter 颤振显示数据并单击进行更改,flutter,dart,Flutter,Dart,我有一个赋值,我需要显示来自API的值,然后按“启动”按钮,然后显示下一个值 这是我的密码 class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { var _questions = new List<Questions>(); _getQuestions() { API.getUsers().
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
var _questions = new List<Questions>();
_getQuestions() {
API.getUsers().then((response) {
setState(() {
Iterable list = json.decode(response.body);
print(list);
print(list);
_questions = list.map((model) => Questions.fromJson(model)).toList();
print(_questions);
});
});
}
initState() {
super.initState();
_getQuestions();
}
final PrimaryColor = const Color(0xff404040);
final PreferredSizeWidget appBar = AppBar(
centerTitle: true,
title: Text(
'Would you Rather',
style: TextStyle(fontFamily: 'FredokaOne'),
),
backgroundColor: PrimaryColor,
);
double stackHeight = (MediaQuery.of(context).size.height -
appBar.preferredSize.height -
MediaQuery.of(context).padding.top);
double stackWidth = MediaQuery.of(context).size.width;
return Scaffold(
backgroundColor: Color(0xff404040),
appBar: appBar,
body: Column(
children: <Widget>[
Container(
child: Text('need to show value here'),
),
RaisedButton(
onPressed: on this press i need to change value,
child: Text('next'),
)
],
));
}
}
我需要显示数据中的值
而不是
,然后单击需要更改而不是
的其他值,您可以按如下方式轻松执行
步骤1:声明变量以保持单击计数并保持显示值
int currentValue = 0;
String value = "";
步骤2:在Click事件中,设置if-else条件,并执行以下操作:
if (currentValue >= _questions.length) {
currentValue = 0;
setState(() {
value = items[currentValue].rather;
});
} else {
setState(() {
value = items[currentValue].rather;
});
currentValue ++;
}
步骤3:在display Text()中设置声明的变量
全身小部件应该是这样的
body: Column(
children: <Widget>[
Container(
child: Text(value),
),
RaisedButton(
onPressed: (){
if (currentValue >= items.length) {
currentValue = 0;
setState(() {
value = items[currentValue];
});
} else {
setState(() {
value = items[currentValue];
});
currentValue ++;
}
}
child: Text('next'),
)
],
));
body:Column(
儿童:[
容器(
子项:文本(值),
),
升起的按钮(
已按下:(){
如果(当前值>=items.length){
currentValue=0;
设置状态(){
值=项目[当前值];
});
}否则{
设置状态(){
值=项目[当前值];
});
currentValue++;
}
}
子项:文本('next'),
)
],
));
child: Text(value)
body: Column(
children: <Widget>[
Container(
child: Text(value),
),
RaisedButton(
onPressed: (){
if (currentValue >= items.length) {
currentValue = 0;
setState(() {
value = items[currentValue];
});
} else {
setState(() {
value = items[currentValue];
});
currentValue ++;
}
}
child: Text('next'),
)
],
));