"However, I have seen a lot of books say that flux only depends on charge enclosed..."
Generally, when textbooks say that flux depends only on the enclosed charge, they are referring to the flux of a closed surface like a sphere or a cube. I assume your question is precisely about how the flux through a cube and a sphere is equivalent.
You can, by all means, do the integration to find flux enclosed by a cube, but the integration becomes a bit tedious given that the electric field isn't a constant and reduces as we move away from the centre towards the corners. You can go the extra mile by all means and ask " well if we put a charge very close to the surface wouldn't $E$ be changing for $dA$? How can their integral, ie flux, be equal???" Soon you realise that where $E$ is scarce on a portion of the closed surface, it is compensated by an excess of $E$ in other regions.
You can think of it this way. Take a spraypainter, the one you use for graffiti. Take it very close to the wall, about $10 cm$ and spray the paint. You notice that the region is painted thickly and darkly, but concentrated in only a small region. Now pull back the spraypainter by about half a meter and spray once again. This time you see that the area being painted increases. But at the same time, the painted region become lighter and thinner.
Image courtesy of Wikipedia
There's also another reason as to why Gauss' law is true. And that's because electric charges obey inverse square law
Had they obeyed inverse cube law, this may not have been possible. See this SE post