【问题标题】:Flutter: How to Disable FlatButton based mysql databaseFlutter:如何禁用基于 FlatButton 的 mysql 数据库
【发布时间】:2020-09-27 15:37:08
【问题描述】:

如何根据数据库中包含的值禁用FlatButton? 我的应用程序显示从具有 4 个答案选项的数据库中提取的 5 个问题。 我的计划是在用户选择答案后禁用该按钮。 怎么处理?

我的功能

_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 接口

<?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);
    }
}

?>

Here's my table, id_soal and id_user are foreign key. If data not exist, then button active else button disabled

【问题讨论】:

    标签: flutter button dart colors


    【解决方案1】:

    禁用按钮的一种方法是在 onPressed 函数上使用 bool 值,如下所示

    `RaisedButton(
      child: Text("PRESS BUTTON"),
      onPressed: booleanCondition
        ? () => myTapCallback()
        : null
    )`
    

    如果您想显示/使用多个答案问题,您可以使用Radio&lt;T&gt; 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;
                  });
                },
              ),
            ),
          ],
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多