我正在尝试使用TextSpan上的TapGestureRecognizer在flutter中的auth页面之间添加导航。一切都设置好了,但单击的文本仍然没有导航到首选页面。
登录用户界面的一部分,我在其中使用TapGestureRecognizer:
RichText(
text: TextSpan(
text: 'No account',
style: TextStyle(color: Colors.black),
children: [
TextSpan(
recognizer: TapGestureRecognizer()
..onTap = () => widget.onClickedSignUp,
text: 'Click Here',
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.black))
]))
登录的第一部分。dart:
class LoginScreen extends StatefulWidget {
final VoidCallback onClickedSignUp;
const LoginScreen({Key? key, required this.onClickedSignUp})
: super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
final navigatorKey = GlobalKey<NavigatorState>();
final formKey = GlobalKey<FormState>();
@override
void dispose() {
emailController.dispose();
passwordController.dispose();
super.dispose();
}
auth.dart:
class _AuthPageState extends State<AuthPage> {
bool isLogin = true;
@override
Widget build(BuildContext context) => isLogin
? LoginScreen(onClickedSignUp: toggle)
: SignUpScreen(onClickedSignIn: toggle);
void toggle() {
setState(() {
isLogin != isLogin;
});
}
}
main.dart:
class MainPage extends StatelessWidget {
const MainPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Something went wrong'));
} else if (snapshot.hasData) {
return HomeScreen();
} else {
return AuthPage();
}
},
));
}
}
如果有人能帮助我,我将感激不尽!
2条答案
按热度按时间fd3cxomn1#
要调用方法,请在函数中使用
()
,并将逻辑更改为
isLogin = !isLogin;
测试片段
emeijp432#
试试这个
或此