Last time I had gnats.. I covered the smaller pots (with coco) with XXL panty hose.
I won't go into how it was kind of embarrassing standing in the women's isle for 30 minutes trying to find the right size while they smirked at me, but I'll tell ya it definitely works to keep the flyers from coming out and spreading. Wrap all the way around the pot and the main stalk, and rubber bandit. You can still water the pot and get runoff through the fabric, but the gnats can't get through it. Soon they will all hatch and turn into flyers, and all die in a week.
For the bigger pots, I had to fool around and cut the panty hose liners into sheets, and used metal tape to hold in on. That includes the bottom of the pots with the drainage holes, because they can get in through there too.
As for the other secret weapon that no one would probably believe me... I let the coco coir dry out almost all the way (bad I know), and tried something crazy that i though might work. I made some peppermint e-cig vape juice with twice the amount of flavoring as normal, and hooked a straw up to my vape devices drip tip. Stuck the straw up through one of the drain holes on the pot. I then blew through the airflow intake and vaped the hell out of the coco until it looked like the whole pot was smoking with fog.
I watched as all the fungus gnat larva wriggled there way to the top of the medium and died within 15 minutes!
I'm pretty sure the gnats came straight out of the bag of premixed amended coco I got from the grow store. There are little vent holes in the bags and the gnats can fly in and lay eggs. Or come infected straight from the factory. Gnat eggs can even go dormant for awhile I'm pretty sure, until you start adding water. I hardly ever use coco (been on break for awhile and usually grow hydro anyway) but when I do, I expand the compressed blocks with boiling water. If your using soil the risk is even higher for already being contaminated with gnat eggs IMO.