gpt4 book ai didi

dart - Flutter 在 RaisedButton 上设置状态 onPressed

转载 作者:IT王子 更新时间:2023-10-29 07:06:19 26 4
gpt4 key购买 nike

我正在构建一个测验应用程序,它会在用户提交他们选择的答案后显示对正确答案的解释。

布局上有两个按钮——“下一个问题”和“提交答案”。

在初始状态下,“下一个问题”按钮很微妙,因为它不可点击,只有“提交答案”按钮可点击。

Click Here to View the Layout of the Initial State

当点击“提交答案”按钮时,会发生两个 Action :
1.“提交答案”按钮变得微妙且不可点击,“下一个问题”按钮变得粗体且充满活力,当然可以点击。
2. 此外,在两个按钮所在的行下方,会出现一个附加部分(可能是另一个容器,我不知道),显示正确答案的解释。

我需要一些帮助来实现上述两个 Action

到目前为止,这是我的代码:

Widget nextQuestion = new RaisedButton(
padding: const EdgeInsets.all(10.0),
child: const Text('Next Question'),
color: Color(0xFFE9E9E9),
elevation: 0.0,
onPressed: () {
null;
},
);

Widget submitAnswer = new RaisedButton(
padding: const EdgeInsets.all(10.0),
child: const Text('Submit Answer'),
color: Color(0xFFE08284),
elevation: 5.0,
onPressed: () {
null;
},
);

return Scaffold(
body: new CustomScrollView(
slivers: <Widget>[
new SliverPadding(
padding: new EdgeInsets.all(0.0),
sliver: new SliverList(
delegate: new SliverChildListDelegate([
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.min,
children: <Widget>[nextQuestion, submitAnswer]),
new SizedBox(height: 50.0),
]),
),
),
],
),
);

最佳答案

您可以使用setState 方法实现。

我实现了类似的东西。

import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
title: 'Demo',
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
));
}


class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
int submit = 0;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
new RaisedButton(
padding: const EdgeInsets.all(10.0),
child: const Text('Next Question'),
color: submit == 0 ? Color(0xFFE9E9E9) : Colors.grey,
elevation: 0.0,
onPressed: () {
submit == 0 ? null : Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
}
),
new RaisedButton(
padding: const EdgeInsets.all(10.0),
child: const Text('Submit Answer'),
color: Color(0xFFE08284),
elevation: 0.0,
onPressed: () {
setState(() {
submit = 1;
});
},
),
],
),
submit == 1 ? new Container(
child: new Text("hello World"),
) : new Container()
],
)
);
}
}



class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}

关于dart - Flutter 在 RaisedButton 上设置状态 onPressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52028112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com