如何根据数据库中包含的值来禁用FlatButton?我的应用程序显示5个问题,这些问题取自具有4个答案选项的数据库。我的计划是在用户选择答案后禁用该按钮。如何处理?
我的功能
_disableButton(BuildContext context, int idSoal, String idUser) async {
final response = await http.post(BaseUrl.cekJawaban, body: {
'id_user': idUser,
'id_soal': "$idSoal",
});
final data = jsonDecode(response.body);
int value = data['value'];
String pesan = data['message'];
if (value == 1) {
print(pesan);
} else {
print(pesan);
}
}
Mysql api
<?php
require "../config/connect.php";
if($_SERVER['REQUEST_METHOD']=="POST"){
$response = array();
$id_user = $_POST['id_user'];
$id_soal = $_POST['id_soal'];
$cek = "SELECT * FROM t_jawab WHERE selesai_jawab ='1' AND id_user='$id_user' AND id_soal='$id_soal'";
$result = mysqli_fetch_array(mysqli_query($conn, $cek));
if (isset($result)){
$response['value']=1;
$response['message']="Question and answer found!";
echo json_encode($response);
mysqli_close($conn);
}else{
$response['value']=0;
$response['message']="Question and answer not found!";
echo json_encode($response);
}
}
?>
一种禁用按钮的方法是在onPressed函数上使用bool值,如下所示
`RaisedButton(
child: Text("PRESS BUTTON"),
onPressed: booleanCondition
? () => myTapCallback()
: null
)`
来自您的问题,如果您想显示/使用多个答案问题,可以使用Radio<T> class
用于在多个互斥值之间进行选择。当选择一个组中的一个单选按钮时,该组中的其他单选按钮将不再被选择。枚举通常用于此目的。
Radio<T> class
example
// Flutter code sample for Radio
// Here is an example of Radio widgets wrapped in ListTiles, which is similar
// to what you could get with the RadioListTile widget.
//
// The currently selected character is passed into `groupValue`, which is
// maintained by the example's `State`. In this case, the first `Radio`
// will start off selected because `_character` is initialized to
// `SingingCharacter.lafayette`.
//
// If the second radio button is pressed, the example's state is updated
// with `setState`, updating `_character` to `SingingCharacter.jefferson`.
// This causes the buttons to rebuild with the updated `groupValue`, and
// therefore the selection of the second button.
//
// Requires one of its ancestors to be a [Material] widget.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: Center(
child: MyStatefulWidget(),
),
),
);
}
}
enum SingingCharacter { lafayette, jefferson }
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
SingingCharacter _character = SingingCharacter.lafayette;
Widget build(BuildContext context) {
return Column(
children: <Widget>[
ListTile(
title: const Text('Lafayette'),
leading: Radio(
value: SingingCharacter.lafayette,
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
});
},
),
),
ListTile(
title: const Text('Thomas Jefferson'),
leading: Radio(
value: SingingCharacter.jefferson,
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
});
},
),
),
],
);
}
}