You need to add a pause within your loop because your current loop is continuously running and not giving the system a chance to process other events, including button presses -
void Screen1View::relay_1()
{
if (toggleButton1.getState()) {
while (1) {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_2, GPIO_PIN_SET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_Delay(1000);
if (!toggleButton1.getState()) {
break; }
}
} else {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
}
}