代码之家  ›  专栏  ›  技术社区  ›  Shady Aziza

PageView:禁用默认滚动并将其替换为Tap事件

  •  87
  • Shady Aziza  · 技术社区  · 8 年前

    如果我有 PageView ,如何禁用默认滚动行为(滑动),并通过点击按钮将下一个项目滚动到视图中?

    5 回复  |  直到 8 年前
        1
  •  259
  •   Burnash    4 年前

    要禁用刷卡,可以设置:

    PageView(physics: NeverScrollableScrollPhysics())
    
        2
  •  12
  •   Community Mohan Dere    6 年前

    因此,我尝试以下方法来解决这个问题,如果有人有更好的解决方案,请与我们分享。

    使按钮转到 PageView :

    添加一个 PageController 到您的 页面视图 ,以及方法 animateTo jumpTo 当用户按下按钮时转到所需项目。我已经将当前视图的整个宽度设置为偏移,以便成功地过渡到下一个项目。

    onPressed: () {
            c.animateTo(MediaQuery
                .of(context)
                .size
                .width, duration: new Duration(seconds: 1),
                curve: Curves.easeIn);
          }
    
     
    

    禁用默认刷卡行为:

    我所做的就是把我的 页面视图 在…内 IgnorePointer 要忽略任何用户交互,我真的不喜欢这个解决方案,在这个示例中它可能工作得很好,但是,在其他情况下,我可能希望用户与当前显示页面中的单个小部件交互。

    这是我的示例代码:

    enter image description here

    class FirstPage extends StatefulWidget {
      @override
      _FirstPageState createState() => new _FirstPageState();
    }
    
    class _FirstPageState extends State<FirstPage> {
      ScrollController c;
    
      @override
      void initState() {
        super.initState();
        c = new PageController();
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          floatingActionButton: new Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.end,
    
            children: <Widget>[
              new FloatingActionButton(
                  child: new Icon(Icons.navigate_before), onPressed: () {
                //c.animateTo(MediaQuery.of(context).size.width, duration: new Duration(seconds: 1), curve: Curves.easeIn);
                c.jumpTo(0.0);
              }),
              new Container(width: 15.0,),
              new FloatingActionButton(
                  child: new Icon(Icons.navigate_next), onPressed: () {
                c.animateTo(MediaQuery
                    .of(context)
                    .size
                    .width, duration: new Duration(seconds: 1),
                    curve: Curves.easeIn);
              }),
            ],),
          appBar: new AppBar(title: new Text("First Page")),
          body: new IgnorePointer(child: new PageView(
            controller: c,
            children: <Widget>[
              new Column (
                  children: <Widget>[new Container (child: new Text("First Item"))
                  ]),
              new Container (child: new Text("Second Item")),
            ],
          ),),
        );
      }
    }
    

    欢迎对此解决方案提出任何建议或修改。

        3
  •  7
  •   GILO    6 年前

    停止滚动的最佳方法是更改页面视图的物理参数

    如果页面视图处于开始或结束使用状态,则停止滚动边缘

    physics: ClampingScrollPhysics()
    

    要完全停止滚动,请使用

    physics: NeverScrollableScrollPhysics()
    
        4
  •  5
  •   Andrew Stevenson    6 年前

    可能对某些人有所帮助的补充说明。

    如果您在其中一个页面中有一个动态GoogleMap,而该页面已经停止了对手势的反应,那么使用PageView(physics:new NeverScrollableScrollPhysics())非常有用。页面视图似乎具有优先权并覆盖了地图。

    添加NeverScrollablePhysics()可以让地图再次对手势做出反应。

    谢谢

        5
  •  1
  •   Sarmad Ashfaq Chaudhary    5 年前

    要禁用手势刷卡,请将以下属性添加到页面视图中:

    PageView(physics:new NeverScrollableScrollPhysics())
    

    要在PageView中导航到下一页/小部件,请将以下代码添加到按钮的onPressed属性:

    _pageController.nextPage(
         duration: Duration(milliseconds: 1000),
         curve: Curves.easeIn
    );