Reaching "flow" (or "in the zone" as some people say) is something that depends on so many variables being just perfect. Some that come to mind:
- motivation to complete(ie, interesting)
- not being interrupted
- physical/emotional state(harder to enter it if you are upset about something or are overly exhausted/sleepy)
- confidence in code (you don't have to sit back and study for 15 minutes before a change)
- "Knowing" you have a block of free time
- Random quantum factors
- probably a lot of other factors
When all of these variables are just right, you might enter "flow" or you might not. The biggest controllable factor you have though is to not be interrupted. If you are in flow, an interruption can completely collapse it and might end your productivity for the day. This is why you see so much content about "do not disturb the programmers!" because an interruption can cause this collapsing of flow.
Also, I mention having a block of free time because it's really hard to get hyper-motivated about something when you have a meeting coming up in an hour. This is why so many programmers advocate for as few meetings and other daily interruptions as possible, or at the very least have them at the beginning of the day. I can never get into the zone when I know I will be interrupted in an hour or two. This is also why I usually don't enter flow until after lunch. On the one occasion I did enter it before lunch I ended up noticing I was a bit hungry at 4pm and had basically lost all track of time from 11pm to 4pm. (Another good tip: keep some snacks at your desk in case this does happen)
Now, even when all these factors are right, you still may not enter flow. I've had one of those "perfect" times when I've sat down to program and just couldn't get into it. Hence, the "random quantum factors". You can't do anything to "enter" flow, all you can do is make it more likely to happen and for it to last longer when it does.