简体   繁体   English

如何在 flutter 中禁用带有页面视图的滑动操作

[英]How to disable swipe action with page view in flutter

I created a sign up screen and I used a pageview in it.我创建了一个注册屏幕,并在其中使用了综合浏览量。 That page view has a controller and that controller also works with a smooth page indicator I added.该页面视图具有 controller 并且 controller 也适用于我添加的平滑页面指示器。 The page view is also connected to a button I created using a container wrapped in a gesture detector.页面视图还连接到我使用包裹在手势检测器中的容器创建的按钮。 I want the page view to only be controlled by the button I created and I want to disable swiping through the different pages using the fingers.我希望页面视图仅由我创建的按钮控制,并且我想禁用使用手指在不同页面之间滑动。 I only want the button to control it我只想要按钮来控制它

this is the screen:这是屏幕:

这里

和这里

this is my code:这是我的代码:

import 'dart:ffi';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:email_validator/email_validator.dart';

class Register extends StatefulWidget {
  final VoidCallback showLogInPage;
  const Register({
    Key? key,
    required this.showLogInPage,
  }) : super(key: key);

  @override
  State<Register> createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  bool _isObscure = true;
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();
  final _confirmPasswordController = TextEditingController();

  @override
  void dispose() {
    _emailController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  late String _email, _password;
  bool value = false;
  final _controller = PageController();
  bool onLastPage = false;

  Future signUp() async {
    if (passwordConfirmed()) {
      FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: _emailController.text.trim(),
          password: _passwordController.text.trim());
    } else {}
  }

  bool passwordConfirmed() {
    if (_passwordController.text.trim() ==
        _confirmPasswordController.text.trim()) {
      return true;
    } else {
      return false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Padding(
          padding: const EdgeInsets.all(20),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 10,
                ),

                GestureDetector(
                  onTap: () => Navigator.of(context).pop(),
                  child: Row(
                    children: [
                      Image.asset(
                        "lib/assets/cancel.png",
                        height: 15,
                        color: Colors.green,
                      ),
                      Expanded(
                          child: Center(
                              child: Text(
                        "Sign Up",
                        style: TextStyle(fontSize: 25),
                      ))),
                    ],
                  ),
                ),

                SizedBox(
                  height: 30,
                ),

                //information

                Column(
                  children: [
                    Text(
                      "Set up your account and manage your",
                      style: TextStyle(fontSize: 21.2, color: Colors.green),
                    ),
                    Text(
                      "inventory with ease",
                      style: TextStyle(fontSize: 21.2, color: Colors.green),
                    ),
                  ],
                ),

                SizedBox(
                  height: 40,
                ),

                Container(
                  height: 300,
                  child:
                      //page view for sign up
                      PageView(
                    onPageChanged: (index) {
                      setState(() {
                        onLastPage = (index == 2);
                      });
                    },
                    controller: _controller,
                    scrollDirection: Axis.horizontal,
                    children: [
                      //enter email page

                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 5),
                        child: Container(
                          child: Column(
                            children: [
                              Text(
                                "Let's start with your email",
                                style: TextStyle(
                                  fontSize: 23,
                                ),
                              ),
                              SizedBox(
                                height: 45,
                              ),
                              TextField(
                                controller: _emailController,
                                obscureText: false,
                                maxLines: null,
                                keyboardType: TextInputType.emailAddress,
                                decoration: InputDecoration(
                                  border: const OutlineInputBorder(),
                                  labelText: "Email Address",
                                  labelStyle: TextStyle(
                                      fontSize: 20, color: Colors.grey),
                                  floatingLabelStyle: TextStyle(
                                      color: Colors.black, fontSize: 20),
                                  hintText: "Email Address",
                                  hintStyle: TextStyle(fontSize: 0.5),
                                  isDense: true,
                                  enabledBorder: OutlineInputBorder(
                                    borderSide: const BorderSide(
                                        width: 2.0, color: Colors.grey),
                                    borderRadius: BorderRadius.circular(7),
                                  ),
                                  focusedBorder: OutlineInputBorder(
                                      borderSide: const BorderSide(
                                          color: Colors.green, width: 2.0),
                                      borderRadius: BorderRadius.circular(7)),
                                ),
                                onChanged: (value) {},
                              ),
                            ],
                          ),
                        ),
                      ),

                      //enter password page
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 5),
                        child: Container(
                          child: Column(
                            children: [
                              Text(
                                "Create a password to secure your account",
                                style: TextStyle(
                                  fontSize: 23,
                                ),
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              TextField(
                                controller: _passwordController,
                                obscureText: _isObscure,

                                // keyboardType: TextInputType.emailAddress,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(),
                                    labelText: "Password",
                                    labelStyle: TextStyle(
                                        fontSize: 20, color: Colors.grey),
                                    floatingLabelStyle: TextStyle(
                                        color: Colors.black, fontSize: 20),
                                    hintText: "Password",
                                    hintStyle: TextStyle(fontSize: 0.5),
                                    isDense: true,
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: const BorderSide(
                                          width: 2.0, color: Colors.grey),
                                      borderRadius: BorderRadius.circular(7),
                                    ),
                                    focusedBorder: OutlineInputBorder(
                                        borderSide: const BorderSide(
                                            color: Colors.green, width: 2.0),
                                        borderRadius: BorderRadius.circular(7)),
                                    suffixIcon: IconButton(
                                        icon: Icon(
                                          _isObscure
                                              ? Icons.visibility_off
                                              : Icons.visibility,
                                          color: Colors.green,
                                        ),
                                        onPressed: () {
                                          setState(() {
                                            _isObscure = !_isObscure;
                                          });
                                        })),
                                onChanged: (value) {},
                              ),
                            ],
                          ),
                        ),
                      ),

                      //confirm pasword page

                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 5),
                        child: Container(
                          child: Column(
                            children: [
                              Text(
                                "Confirm your password",
                                style: TextStyle(
                                  fontSize: 23,
                                ),
                              ),
                              SizedBox(
                                height: 45,
                              ),
                              TextField(
                                controller: _confirmPasswordController,
                                obscureText: _isObscure,

                                // keyboardType: TextInputType.emailAddress,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(),
                                    labelText: "Confirm Password",
                                    labelStyle: TextStyle(
                                        fontSize: 20, color: Colors.grey),
                                    floatingLabelStyle: TextStyle(
                                        color: Colors.black, fontSize: 20),
                                    hintText: "Password",
                                    hintStyle: TextStyle(fontSize: 0.5),
                                    isDense: true,
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: const BorderSide(
                                          width: 2.0, color: Colors.grey),
                                      borderRadius: BorderRadius.circular(7),
                                    ),
                                    focusedBorder: OutlineInputBorder(
                                        borderSide: const BorderSide(
                                            color: Colors.green, width: 2.0),
                                        borderRadius: BorderRadius.circular(7)),
                                    suffixIcon: IconButton(
                                        icon: Icon(
                                          _isObscure
                                              ? Icons.visibility_off
                                              : Icons.visibility,
                                          color: Colors.green,
                                        ),
                                        onPressed: () {
                                          setState(() {
                                            _isObscure = !_isObscure;
                                          });
                                        })),
                                onChanged: (value) {},
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                Center(
                  child: SmoothPageIndicator(
                    controller: _controller,
                    count: 3,
                    effect: ExpandingDotsEffect(activeDotColor: Colors.green),
                  ),
                ),

                SizedBox(
                  height: 20,
                ),

                //continue button
                onLastPage
                    ? GestureDetector(
                        onTap: signUp,
                        child: Container(
                          child: Center(
                            child: Padding(
                              padding: const EdgeInsets.all(20),
                              child: Text(
                                "Sign Up",
                                style: TextStyle(
                                    fontSize: 19, color: Colors.white),
                              ),
                            ),
                          ),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(50),
                              color: Colors.green),
                        ),
                      )
                    : GestureDetector(
                        onTap: () {
                          _controller.nextPage(
                              duration: Duration(milliseconds: 400),
                              curve: Curves.easeIn);
                        },
                        child: Container(
                          child: Center(
                            child: Padding(
                              padding: const EdgeInsets.all(20),
                              child: Text(
                                "Continue",
                                style: TextStyle(
                                    fontSize: 19, color: Colors.white),
                              ),
                            ),
                          ),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(50),
                              color: Colors.green),
                        ),
                      ),

                SizedBox(
                  height: 30,
                ),

                //Already have an account? Sign in
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "Already have an account?",
                      style: TextStyle(fontSize: 16),
                    ),
                    GestureDetector(
                      onTap: widget.showLogInPage,
                      child: Text(
                        " Sign in",
                        style: TextStyle(color: Colors.green, fontSize: 17),
                      ),
                    )
                  ],
                ),

                SizedBox(
                  height: 20,
                ),

                Row(children: [
                  Expanded(
                      child: Container(height: 1, color: Colors.grey[500])),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 5),
                    child: Text('Or continue with:'),
                  ),
                  Expanded(
                      child: Container(height: 1, color: Colors.grey[500])),
                ]),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Container(
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey.shade600),
                          borderRadius: BorderRadius.circular(5)),
                      height: 70,
                      child: Image.asset(
                        "lib/assets/google.png",
                        height: 40,
                      ),
                    ),
                    Text("Or"),
                    Container(
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey.shade600),
                          borderRadius: BorderRadius.circular(5)),
                      height: 70,
                      child: Image.asset(
                        "lib/assets/facebook.png",
                        height: 40,
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

To disable swiping you can set:要禁用刷卡,您可以设置:

PageView(physics: NeverScrollableScrollPhysics())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 flutter 中实现刷卡并显示来自 Firestore 的交易 - How to implement the card swipe and display its transaction from firestore in flutter Flutter:- 此 AdWidget 已在 Widget 树中。 如何禁用此异常。 这是什么意思? - Flutter :- This AdWidget is already in the Widget tree. How to disable this exception. And what does it mean? FLUTTER:如何显示用户特定页面? - FLUTTER : How to display user specific Page? 如何在 flutter 的注册/注册页面中包含重要功能 - How to include important features to a sign up/register page with flutter Flutter 如何从推送通知打开页面,上下文问题 - Flutter how to open page from push notification, context issue 如何在Flutter中谷歌登录后重定向到另一个页面 - How to redirect to another page after google login in Flutter 如何在页面处于活动状态时仅调用一次 function flutter - How to call a function only once while the page is active in flutter Flutter:当通知从 FirebaseMessaging 到达时如何打开应用程序(无需用户操作) - Flutter: How to open the App when notification arrives from FirebaseMessaging (without user action) 在 flutter 调试版本中禁用分析的规范方法 - Canonical way to disable analytics in flutter debug build 用于电子邮件链接身份验证的 flutter ERROR_INVALID_ACTION_CODE 中的 firebase 身份验证 - firebase authentication in flutter ERROR_INVALID_ACTION_CODE for email link authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM