jeudi 18 février 2021

How to use setState in FutureBuilder properly?

I'm making a page which contains some dropboxes which interact each other and a Futurebuilder wrapped ListView.

I'm calling 'setState' when Dropbox changes and checkbox is clicked. However, because of the setState in checkbox's onChanged, the Futurebuilder keeps being called and the ListView is rebuilded. Therefore the entire Listview is blinkning when checkbox is clicked like the video below.

Problem Video

I want to keep the Listview and update only checkbox. Is there anyone who can help me? Thank you.

The full code is

class _StatefulDialogWidgetState extends State<StatefulDialogWidget> {
  ....   

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Dropdown's
        Dropdown(0, widget.ReceiveArgs, _lListOfDepthList),
        Dropdown(1, widget.ReceiveArgs, _lListOfDepthList),
        Dropdown(2, widget.ReceiveArgs, _lListOfDepthList),
        Dropdown(3, widget.ReceiveArgs, _lListOfDepthList),
        Dropdown(4, widget.ReceiveArgs, _lListOfDepthList),
        
        // Listview with FutureBuilder
        AptListview(),
      ],
    );
  }

ListView Code

Widget AptListview() {
    return FutureBuilder<List<String>>(
        future: AptNameListView(widget.ReceiveArgs),
        builder: (context, snapshot) {
          if (_bLastDepth == false) {
            return Text("Select Address");
          } else {
            if (snapshot.hasData == false || snapshot.data.isEmpty == true) {
              return CircularProgressIndicator();
            } else {
              return Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: _AptNameList.length,
                  itemBuilder: (context, index) {
                    //return new Text("${_AptName[index]}");
                    return CheckboxListTile(
                      title: Text(_AptNameList[index]),
                      value: _isAptChecked[index],
                      onChanged: (value) {
                        setState(() {                   //  SetState in FutureBuilder
                          _isAptChecked[index] = value;                              
                        });
                      },
                    );
                  },
                ),
              );
            }
          }
        });
  }

Dropdown Code

Widget Dropdown(int nDepth, ArgumentClass ReceiveArgs,
      List<List<String>> ListOfDepthList) {
    String _Value = "";
    List<DropdownMenuItem<String>> _itemList = null;
    if (ListOfDepthList.length <= nDepth) {
      _Value = "";
      _itemList = null;
    } else {
      _Value = _SelectedAddressList[nDepth];
      _itemList = GetMainItem(ListOfDepthList[nDepth]);
    }
    return DropdownButton(
        value: _Value,
        items: _itemList,
        onChanged: (value) {
          if (value.compareTo(GlobalObject().startMessage) != 0) {
            setState(() {
              .....
              // setState in Dropdown
            });
          }
        });
  }



Check if a submitted checkbox was checked or not in WooCommerce

In WooCommerce, I am trying to check if submitted checkbox is checked or not in the code below:

add_filter('kco_wc_api_request_args', 'krokedil_add_required_checkbox');

function krokedil_add_required_checkbox( $create ) {
    $create['options']['additional_checkbox']['text'] = 'Prenumerera på nyhetsbrevet';
    $create['options']['additional_checkbox']['checked'] = true;
    $create['options']['additional_checkbox']['required'] = false;
    return $create;
}

add_action( 'kco_wc_confirm_klarna_order', 'my_add_kco_order_data', 10, 2 );
function my_add_kco_order_data($order_id, $klarna_order ) {
    if( $klarna_order['additional_checkbox']['checked'] ) {
        add_post_meta( $order_id, 'mailchimp_woocommerce_is_subscribed', 1 );
    } else {
        add_post_meta( $order_id, 'mailchimp_woocommerce_is_subscribed', 0 );
    }   
}

But I can't get it to work. Feels like I've tried all combinations.

Any suggestions?




reset/uncheck checkbox how to reset value in jquery?

I am trying to have a checklist with 4 elements A B C D (value=1 each) and an element E (value=3) to create a "discount" if the user selects this. The value is used as multiplier to create a quote based on other selected criteria.. So I need this to be A+B+C+D=4 but if I press E they all uncheck and the value becomes 3.. however...

With the function I managed to uncheck the boxes but seems like the value won't reset?

    function totalIt() {


        var input = document.getElementsByName("type");
      var multiplier = 0;
      for (var i = 0; i < input.length; i++) {
        if (input[i].checked) {
          multiplier += parseFloat(input[i].value);
        }
      }

        $('#family').on('click', function() {
    $('.font').not(this).removeAttr('checked')
var multiplier = 3;
});


    console.log(multiplier)

};
<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="description" content="" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script type="text/javascript" src="scroll.js"></script>

    <style>
        .column2{
            display: flex;
            flex-wrap: wrap;

        }
        .theForm{
            width: 30%;}
    </style>
</head>

<!-- BODY  -->


<body>
            <form action="" class="theForm">
             <fieldset>
                     <legend>
                             SELECT 
                     </legend>
                     <label><br>
                             <input class="font"name="type" value="1" type="checkbox" id="p1" onclick="totalIt()"/>
                             Thin
                     </label>
                     <label><br>
                             <input class="font"name="type" value="1" type="checkbox" id="p2" onclick="totalIt()"/>
                             Regular
                     </label>
                     <label><br>
                             <input class="font"name="type" value="1" type="checkbox" id="p3" onclick="totalIt()"/>
                             Medium
                     </label>
                     <label><br>
                             <input class="font"name="type" value="1" type="checkbox" id="p4" onclick="totalIt()"/>
                             Bold
                     </label>
                     <label><br>
                             <input  id="family" name="type" value="3" type="checkbox" id="p5" onclick="totalIt()"/>
                             Family
                     </label>

             </fieldset>

        </form>




    

</body>

</html>



Slidable widget taking too much size flutter

I'm working on a todo App and I display my todos with some kinda cards and I'm using the slidable widget to modify or delete them. The problem is that the left slide takes too much space and is above my checkbox. That's a picture of it:

slidable

slidable 2

I don't really know why it's like that so if you have some idea please tell me.

 return ClipRRect(
  borderRadius : BorderRadius.circular(15),
  child: Slidable(
    actionPane: SlidableDrawerActionPane(),
    key: Key(widget.todo.id),
    actions: [
      IconSlideAction(
        color: Colors.green,
        onTap: () {},
        caption: 'Edit',
        icon: Icons.edit,
      )
    ],
    secondaryActions: [
      IconSlideAction(
        color: Colors.red,
        caption: 'Delete',
        onTap: () {},
        icon: Icons.delete,
      )
    ],



mercredi 17 février 2021

submit is enabled only if first checkbox is checked [duplicate]

I want to enable submit button when at least one of the checkboxes (which is inside a for loop) is checked.

Currently what happens is, the submit button is enabled only when the first checkbox is checked, if I check the second or third checkbox without selecting the first one it won't enable the submit button.

function EnableSubmitBtn() {

  if (document.getElementsByName("AccountChecked").checked == false) {
    document.getElementById("btnSubmit").disabled = true;
  } else {
    if (document.getElementById("fileSupport").files.length != 0) {
      document.getElementById("btnSubmit").disabled = false;
    }
  }
}
<td>
  <input onclick="EnableSubmitBtn()" name="AccountChecked" id="chkBoxAccNo" type="checkbox" value="@item.accnt_no" style="background:transparent;color:white" />
</td>



WPF Checkbox Custom Style Check disappeared

Maybe this is redundant but I could not find anything. I probably am missing something simple.

The checkbox in question is set in the codebehind on a selection event

  chkEmpOK.IsChecked = _cfgUsr.IsEnabled;

This worked as desired until I tried to add a custom style. Now the checkbox displays the text but the image/glyph/box part of the checkbox does not display. I am assuming I did something wrong in the style. Here is the control and style from the XAML:

<CheckBox x:Name="chkEmpOK" Grid.Column="2" Grid.Row="4" Margin="10,10,0,0">
    <CheckBox.Style>
        <Style TargetType="{x:Type CheckBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter Property="Content" Value="Click Me to Enable"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Content" Value="To remove access- click me"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                        <ContentPresenter Content="{TemplateBinding Content}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </CheckBox.Style>
</CheckBox>

This is what it looks like Snippet from UI

Hopefully I am missing something simple. Does it have anything to do with it inheriting from ToggleButton?




Advanced Custom Fields - Create a 'Room' options list with multiple sections that is maintained from the Options tab

I have a list of “Room” options set up in the Options tab. I want the client to be able to manage the list from Options and then the list to appear on the Rooms page so they can select which options are available for that particular room. If the client update the main list of options form the Options page, they would need to update on the Rooms page.

I have a Repeater called “Room features / room_features” already set up and is working as expected on the Options page.

What is shown in the Options page:

Repeater:
Row 1:
Text – Title – ‘Beds’<--- This is basically a label for that section and is only editable from the Options page.
Repeater – Options: 2 beds, 3 beds etc…
Row 2:
Text – Title – ‘Size’<--- This is basically a label for that section and is only editable from the Options page.
Repeater – Options: 340 Square Feet , 640 Square Feet etc…

What I would like to show on the Rooms page:

Group:
Set 1: each set built from the Repeater
Visibility – Yes/No
Title – Beds – Not editable
Options – Checkboxes created by the Repeater

Set 2: each set built from the Repeater
Visibility – Yes/No
Title – Beds – Not editable
Options – Checkboxes created by the Repeater

Below is the code I have so far. The rows are being built and the title is being populated. "var_dump($options_value);" is outputting the repeater values that would create the checkboxes but I can't figure out how to add the choices. key=field_602ad24de4cfd is the repeater on the Rooms page.

function my_acf_set_repeater( $value, $post_id, $field ){
    
    $value = array();    
    
    // this is the main repeater from Options
    $settings_values = get_field('room_features','option');
    
    foreach( $settings_values as $settings_value ){
        // This is setting the title
        $value[]= array('field_602c2e8c5f28f' => $settings_value['title']);
        
        // All of the options
        $options_values =  $settings_value['items'];

        foreach( $options_values as $options_value ){
            var_dump($options_value);
        }
    }

    return $value;
}

add_filter('acf/load_value/key=field_602ad24de4cfd', 'my_acf_set_repeater', 10, 3);

Is the 1st image below you can see the 1st 3 arrays are what's in row 1 and the other 2 are in row 2. That is what is showing on the Rooms page.

The second image is how the Options page looks now.

Is this even possible? Thanks in advance!!!

enter image description here

enter image description here