TapGestureRecognizer在 Flutter 中不工作

lyr7nygr  于 2022-11-30  发布在  Flutter
关注(0)|答案(2)|浏览(133)

我正在尝试使用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();
        }
      },
    ));
  }
}

如果有人能帮助我,我将感激不尽!

fd3cxomn

fd3cxomn1#

要调用方法,请在函数中使用()

TextSpan(
   recognizer: TapGestureRecognizer()
     ..onTap = () => widget.onClickedSignUp(),

并将逻辑更改为isLogin = !isLogin;
测试片段

class MainPage extends StatelessWidget {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: AuthPage());
  }
}

class AuthPage extends StatefulWidget {
  const AuthPage({super.key});

  @override
  State<AuthPage> createState() => _AuthPageState();
}

class _AuthPageState extends State<AuthPage> {
  bool isLogin = true;
  void toggle() {
    debugPrint("yay");
    setState(() {
      isLogin = !isLogin;
    });
  }

  @override
  Widget build(BuildContext context) => Column(
        children: [
          Text("$isLogin"),
          SizedBox(
            height: 400,
            width: 400,
            child: isLogin ? LoginScreen(onClickedSignUp: toggle) : Text("F"),
          )
        ],
      );
}

class LoginScreen extends StatefulWidget {
  const LoginScreen({Key? key, required this.onClickedSignUp})
      : super(key: key);

  final VoidCallback onClickedSignUp;

  @override
  State<LoginScreen> createState() => _LogInScreenState();
}

class _LogInScreenState extends State<LoginScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: 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))
          ])),
    );
  }
}
emeijp43

emeijp432#

试试这个

..onTap = () => widget.onClickedSignUp.call(),

或此

..onTap = widget.onClickedSignUp,

相关问题