The best way, is to first fill up some xxxxxl panty hose liners up with the gnat infested soil, and then stuff that in your pot. Once the plant gets big enough, put a rubber band around the opening of the liner and the stalk, so it can stretch as it grows. You can water and get drainage through the fabric, but gnats can't fly in and out, ever. Smaller pots can fit right inside of the hose. Bonus, you don't get dirty soil all over, ewww!
Ok haha ya i know, but for real... Panty liner fabric cut up into sections & taped around the top rim of the pot works good too, for larger pots. Gotta put little patches over the drain holes as well, because they'll fly in and out from there too. Many ways to make it work, and completely seal off the soil\medium from flyers. Use a water proof tape, and good to go!