代码之家  ›  专栏  ›  技术社区  ›  dipansh

制作带有滑动标题的水平页面视图

  •  0
  • dipansh  · 技术社区  · 3 年前

    我正在尝试在Flutter中实现一个水平页面视图。我发现实现页面视图很容易,但我不知道如何在以下UI中更改标题:

    enter image description here

    例如,当点击下一个箭头时,我想显示带有默认动画的下一页,我还想将标题更新为类似的内容 2. Page 2 使用相同的动画,但不移动箭头。
    我怎样才能做到这一点?

    0 回复  |  直到 3 年前
        1
  •  0
  •   amir_a14    3 年前

    使用以下代码示例:

    结果:

    flutter page view with next previous button

    import 'dart:ui';
    import 'package:flutter/material.dart';
    void main() {
      runApp(const MyApp2());
    }
    
    class MyApp2 extends StatefulWidget {
      const MyApp2({Key? key}) : super(key: key);
    
      @override
      State<MyApp2> createState() => _MyApp2State();
    }
    
    class _MyApp2State extends State<MyApp2> {
      var pageController = PageController();
      late num currentPage = pageController.initialPage;
      var titleController = PageController();
      var pageTitles = [
        "1. Login Page",
        "2. Profile Page",
        "3. Products Page",
        "4. Settings Page",
      ];
    
      @override
      void initState() {
        super.initState();
        pageController.addListener(() {
          currentPage = pageController.page ?? 0;
          if (currentPage is int) {
            setState(() => {}); //for hiding next or previous button when we are in last or first page
          }
          titleController.position.correctPixels(pageController.offset *
              titleController.position.viewportDimension /
              pageController.position.viewportDimension);
          titleController.position.notifyListeners();
        });
      }
    
      void nextPage() {
        pageController
            .animateToPage(
              (pageController.page ?? 0).toInt() + 1,
              duration: kThemeAnimationDuration,
              curve: Curves.decelerate,
            )
            .then((value) => setState(() => {})); //for hiding next button when we are in last page
      }
    
      void previousPage() {
        pageController
            .animateToPage(
              (pageController.page ?? 0).toInt() - 1,
              duration: kThemeAnimationDuration,
              curve: Curves.decelerate,
            )
            .then((value) => setState(() => {})); //for hiding previous button when we are in first page
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          scrollBehavior: ScrollConfiguration.of(context)
              .copyWith(dragDevices: {PointerDeviceKind.mouse, PointerDeviceKind.trackpad, PointerDeviceKind.touch}),
          home: Scaffold(
            appBar: AppBar(
              backgroundColor: const Color(0xff221D28),
              title: Row(
                children: [
                  if (currentPage == 0) //hide previous button
                    const SizedBox(width: 32)
                  else
                    InkResponse(
                      onTap: previousPage,
                      child: const Icon(Icons.chevron_left, color: Colors.white, size: 32),
                    ),
                  Expanded(
                    child: SizedBox(
                      height: kToolbarHeight,
                      child: IgnorePointer(
                        child: PageView.builder(
                          controller: titleController,
                          itemCount: pageTitles.length,
                          itemBuilder: (c, i) => Center(
                            child: Text(pageTitles[i], style: const TextStyle(color: Colors.white)),
                          ),
                        ),
                      ),
                    ),
                  ),
                  if (currentPage == pageTitles.length - 1) //hide next button
                    const SizedBox(width: 32)
                  else
                    InkResponse(
                      onTap: nextPage,
                      child: const Icon(Icons.chevron_right, color: Colors.white, size: 32),
                    ),
                ],
              ),
            ),
            body: PageView.builder(
              controller: pageController,
              itemCount: pageTitles.length,
              itemBuilder: (c, i) => Center(child: Text(pageTitles[i])),
            ),
          ),
        );
      }
    }