dart 防止PageView.builder滑动到最大页面

ruyhziif  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(179)

我正在尝试测试在这个软件包中找到的PageView.builder:平滑页面指示器:^1.0.1
页面视图

child: PageView.builder(
                  controller: controller,
                  // itemCount: pages.length,
                  itemBuilder: (_, index) {
                    return pages[index % pages.length];
                  },
                ),
              ),

页数

final pages = List.generate(
        6,
        (index) => Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16),
                color: Colors.grey.shade300,
              ),
              margin: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
              child: Container(
                height: 280,
                child: Center(
                    child: Text(
                  "Page $index",
                  style: TextStyle(color: Colors.indigo),
                )),
              ),
            ));

主计长

final controller = PageController(viewportFraction: 0.8, keepPage: true);

如果是第一页,向左滚动将被禁用。但我想尝试禁用滚动一旦我达到最大页。我将如何实现这一点?

bogh5gae

bogh5gae1#

我正在尝试解决您的问题:
代码如下:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

List<String> dataList = ["A", "B", "C", "D", "E", "F", "G"];

int pointer = 0;

class Swipefunctions extends StatefulWidget {
  const Swipefunctions({Key? key}) : super(key: key);
  @override
  State<Swipefunctions> createState() => _SwipefunctionsState();
}

class _SwipefunctionsState extends State<Swipefunctions> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("THIS IS APPBAR"),
        ),
        body: PageView.builder(
            onPageChanged: (int index) {
              print("prinetd");
              setState(() {
              pointer++;
              pointer=index;
              });
            },
            itemCount: dataList.length,
            itemBuilder: (BuildContext context, int index) {
              return MyPage(index: index,);
            })
    );
  }
}

class MyPage extends StatefulWidget {
  final int index;

  const MyPage({super.key, required this.index});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {


  @override
  Widget build(BuildContext context) {
    print("pointer at $pointer");
    return Scaffold(
      body: Container(
        margin: const EdgeInsets.all(15.0),
        padding: const EdgeInsets.all(3.0),
        decoration: BoxDecoration(
            border: Border.all(color: Colors.blueAccent)
        ),
        child: Center(
          child: Text(dataList[pointer], style: const TextStyle(fontSize: 30),),
        ),
      ),
    );

  }
}
iovurdzv

iovurdzv2#

我尝试做的是禁用PageView.builder的滚动功能,然后在Scaffold上添加一个手势检测器来检测我是否在向左/向右滑动。然后使用PageView.builder的控制器来滚动。

相关问题