We can do that already, in fact, using a lovely bit of physics/ materials science called ferroics, we can do it with a switch. If the switch is electrical, specifically it’s called “piezoelectrics”. If the material starts out transparent, then you change the electrical current going through it, it changes the arrangement of the molecules in the structure and it becomes opaque (or transclcent depending on what it is). When you then turn off the electric it reverts back to transparent.
We’ve just had a new window put into our operations room at work which you can turn opaque at the flick of a switch! It’s quite cool. I think there are other ways like using heat and stress but often these are permanent and not as cool piezoelectric glass.