我正在尝试将我的TextFormField
逻辑转移到钩子上,但我正在尝试的是似乎不起作用。
我的自定义钩子,
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
enum Index { email, password }
TextFormField useTextFormField(Index index) {
return use(_CustomHook(index: index));
}
class _CustomHook extends Hook<TextFormField> {
final Index index;
_CustomHook({@required this.index});
@override
_CustomHookState createState() => _CustomHookState();
}
class _CustomHookState extends HookState<TextFormField, _CustomHook> {
Index index;
@override
TextFormField build(BuildContext context) {
final visible = useState(false);
// useEffect((){
// print('in');
//visible.value=!visible.value;
// print(visible.value.toString());
// return;
// },const []);
return TextFormField(
decoration: InputDecoration(
hintText: 'some help',
suffixIcon: IconButton(
onPressed: () {
visible.value = !visible.value;}, //<--- here
icon: Icon(visible.value ? Icons.visibility : Icons.visibility_off),
)),
validator: (text) {
if (text.isEmpty) return 'enter something';
return null;
},
obscureText: index == Index.email ? visible.value : true,
);
}
}
我是钩子的新手,我不知道我可以使用useState()
并且useEffect()
,如果有人可以提供帮助