Where to put custom widget file?

If you created a custom widget, created a file by example class.my-widget.php containing the class definition of the custom widget, something like this:

class my_widget extends WP_Widget {

    public function __construct() {
        //logic here
    }

    public function widget($args, $instance) {
        //logic here
    }

    public function form($instance) {
        //logic here
    }

    public function update($new_instance, $old_instance) {
        //logic here
    }

}
//registering my widget so its available in the back-end
add_action('widgets_init', function() {
    register_widget('my_widget');
});

and the file its inside a folder in your theme like this:

enter image description here

you have to load that file, to do that you have to add this to your functions.php:

//lets define a constant for the URL to your theme folder
define('YOUR_THEME_FOLDER_PATH', trailingslashit(get_template_directory(__FILE__)));
//lets load the custom widget
require_once (YOUR_THEME_FOLDER_PATH . 'inc/class.my-widget.php');

the inc/ part can be updated to the folder the file is.