【问题标题】:How To Scroll To The Next Page PageView Flutter如何滚动到下一页 PageView Flutter
【发布时间】:2020-03-13 15:13:22
【问题描述】:

我有一个使用页面视图构建器构建的水平表单。我想知道如何在不使用滚动物理的情况下滚动到下一页。这是我的代码

import 'package:flutter/material.dart';
import 'package:flutter_repo/screens/login.dart';
import 'package:flutter_repo/utils/beziercontainer.dart';

class SignUpPage extends StatefulWidget {
    SignUpPage({Key key, this.title}) : super(key: key);

    final String title;

    @override
    _SignUpPageState createState() => _SignUpPageState();
 }

 class _SignUpPageState extends State<SignUpPage> {
     PageController pageController;


     @override
     void initState() { 
     pageController = PageController(initialPage: 0);

     super.initState();

  }

  @override
  void dispose() {
     pageController.dispose();
     super.dispose();
  }

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

   onNext(){
    if (pageController.hasClients) {
    pageController.animateToPage(2, duration: Duration(milliseconds: 300), curve: Curves.easeIn); 
  }
 }

 onComplete(){

 }

 Widget _registrationFields() {
    return PageView(
    physics: NeverScrollableScrollPhysics(),
    controller: pageController,
    children: <Widget>[
    Padding(
      padding: const EdgeInsets.symmetric(horizontal: 14.0),
      child: Column(
        children: <Widget>[
          _entryField("First Name"),
          SizedBox(
            height: 20,
          ),
          _submitButton(text: 'Continue', function: onNext())
        ],
      ),
    ),
     Padding(
       padding: const EdgeInsets.symmetric(horizontal: 14.0),
       child: Column(
        children: <Widget>[
          _entryField("Last Name"),
          SizedBox(
            height: 20,
          ),
          _submitButton(text: 'Continue', function: onNext())
        ],
    ),
     ),
    Padding(
      padding: const EdgeInsets.symmetric(horizontal: 14.0),
      child: Column(
        children: <Widget>[
          _entryField("Email Address"),
           SizedBox(
            height: 20,
          ),
           _submitButton(text: 'Continue', function: onNext())
        ],
      ),
    ),
    Padding(
      padding: const EdgeInsets.symmetric(horizontal: 14.0),
      child: Column(
        children: <Widget>[
          _entryField("Password", isPassword: true),
          SizedBox(
              height: 20,
            ),
             _submitButton(text: 'Continue', function: onNext())
        ],
      ),
    ),
     Padding(
      padding: const EdgeInsets.symmetric(horizontal: 14.0),
      child: Column(
        children: <Widget>[
          _entryField("Confirm Password", isPassword: true),
          SizedBox(
              height: 20,
            ),
             _submitButton(text: 'Continue', function: onNext())
        ],
      ),
    ),
      Padding(
      padding: const EdgeInsets.symmetric(horizontal: 14.0),
      child: Column(
        children: <Widget>[
          _entryField("Set PIN", isPassword: true),
          SizedBox(
              height: 20,
            ),
             _submitButton(text: 'Register', function: onNext())
        ],
      ),
    ),
  ],
);
}

}

我想在点击按钮时动画到下一页。但它根本不起作用。我怎样才能让它工作?现在它什么也没做。为简洁起见,我已经抽象出一些不必要的代码。

【问题讨论】:

  • 您尝试过以下答案吗?如果它解决了您的问题,请将其标记为正确,并考虑为可见性投票。

标签: flutter


【解决方案1】:

您的代码中包含太多变量和方法,您没有共享,我们无法重复使用。所以我创建了一个以编程方式更改页面的示例:

class PageView60672934 extends StatefulWidget {
  @override
  _PageView60672934State createState() => _PageView60672934State();
}

class _PageView60672934State extends State<PageView60672934> {
  PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        physics: NeverScrollableScrollPhysics(),
        controller: _pageController,
        children: <Widget>[
          Center(
            child: Text('Page 1'),
          ),
          Center(
            child: Text('Page 2'),
          ),
          Center(
            child: Text('Page 3'),
          ),
        ],
      ),
      bottomNavigationBar: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          RaisedButton(
            onPressed: previousPage,
            child: Text('Previous'),
          ),
          RaisedButton(
            onPressed: nextPage,
            child: Text('Next'),
          )
        ],
      ),
    );
  }

  void nextPage(){
    _pageController.animateToPage(_pageController.page.toInt() + 1,
      duration: Duration(milliseconds: 400),
      curve: Curves.easeIn
    );
  }

  void previousPage(){
    _pageController.animateToPage(_pageController.page.toInt() -1,
      duration: Duration(milliseconds: 400),
      curve: Curves.easeIn
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2021-12-15
    • 2019-11-25
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多