How to include checkbox in widget backend form?

I’m trying to include a checkbox in my widget back end. But I cannot get the value (on or off) after the user submits it. I thought the value would be stored in “esc_attr( $check )” (as it is when using a text input), but I cannot retrieve it.

This is what I’m trying now:

public function form( $instance ) {
    $check = isset( $instance[ 'check' ] ) ? $instance[ 'check' ] : 'off';
    echo esc_attr( $check ); // If the input is type text it outputs the value
    <input class="widefat" id="<?php echo $this->get_field_id( 'check' ); ?>" name="<?php echo $this->get_field_name( 'check' ); ?>" type="checkbox" />

How can I get this to work? Also how do I get the value of the checkbox in the front end?

Solutions Collecting From Web of "How to include checkbox in widget backend form?"