I’m building a VPN application with flutter. After I click connect, the color of the button already change from blue to red. But when I click disconnect, the color of the button didn’t turn back to blue.
This is my code:
bool isPressed = true;
ElevatedButton(
onPressed: () async {
if (state == FlutterVpnState.disconnected) {
FlutterVpn.connectIkev2EAP(
server: _addressController.text,
username: _usernameController.text,
password: _passwordController.text,
);
setState(() {
isPressed = !isPressed;
},
);
}
if (state == FlutterVpnState.connected) {
FlutterVpn.disconnect();
}
if (state == FlutterVpnState.error) {
FlutterVpn.disconnect();
}
},
child: Text(
state == FlutterVpnState.disconnected? 'Connect' : 'Disconnect',
),
style: ElevatedButton.styleFrom(
primary: isPressed ? Colors.blue : Colors.redAccent
),
),
My question is how to turn back the color to blue?
Thank you in advance for any help.
3
Answers
Try using
MaterialStateProperty
:try with this code.
You need to set a bool for offline and online, then just changed the color based on that bool using theme.