【发布时间】:2022-01-03 23:26:43
【问题描述】:
我有一个如下所示的登录屏幕代码,其中有一个文本按钮,可将登录按钮的状态更改为注册或反向,并希望将其重写为使用GetX 库。但我不知道怎么做?
enum AuthMode { Signup, Login }
class AuthenticationScreen extends StatelessWidget {
const AuthenticationScreen({Key? key}) : super(key: key);
AuthMode _authMode = AuthMode.Login;
@override
Widget build(BuildContext context) {
final GlobalKey<FormState> _formKey = GlobalKey();
void _switchAuthMode() {
if (_authMode == AuthMode.Login) {
setState(() {
_authMode = AuthMode.Signup;
= });
_controller!.forward();
} else {
setState(() {
_authMode = AuthMode.Login;
});
_controller!.reverse();
}
}
return Scaffold(
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 400),
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
,
TextButton(
child: Text(
'${_authMode == AuthMode.Login ? 'SIGNUP' : 'LOGIN'} '),
onPressed: _switchAuthMode,
style: TextButton.styleFrom(
padding:
const EdgeInsets.symmetric(horizontal: 30.0, vertical: 4),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
textStyle: TextStyle(color: Theme.of(context).primaryColor),
),
],
),
),
),
);
}
}
我尝试了一些更改,例如将Authmode 转移到扩展GetxController 的auth_controller 文件并在AuthMode _authMode = AuthMode.Login; 之后添加obs 并尝试在_switchAuthMode() 中使用obx(()=>),但没有成功。
【问题讨论】:
标签: state setstate flutter-getx obs