Preheat oven to 375°F
Grease a baking dish
Peel and thinly slice potatoes
Slice onion thinly, if using
Melt butter in a saucepan
Stir in flour and cook briefly
Whisk in milk or cream until smooth
Add salt, pepper, and optional garlic
Cook sauce until slightly thickened
Layer potatoes in the baking dish
Add onion between layers, if using
Pour sauce evenly over the potatoes
Sprinkle cheese on top, if desired
Cover with foil
Bake until potatoes are tender
Remove foil and bake until top is golden
Let rest before serving
