我使用Page_Flip小部件创建了一个读书页面,但当用户离开应用程序并重新进入图书页面时,我希望它从停止的地方继续.我怎样才能保留用户的信息?我try 了OnFlip、CurrentPage等功能,但我使用的小工具不支持它们.

// HomeScreen.dart

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

final _controller = GlobalKey<PageFlipWidgetState>();

class AChristmasCarol extends StatefulWidget {
  const AChristmasCarol({super.key});

  @override
  State<AChristmasCarol> createState() => _AChristmasCarolState();
}

class _AChristmasCarolState extends State<AChristmasCarol> {
  final List<String> imagePaths = [
    'assets/books/A Christmas Carol/page 1.png',
    'assets/books/A Christmas Carol/page 2.png',
    'assets/books/A Christmas Carol/page 3.png',
    'assets/books/A Christmas Carol/page 4.png',
    'assets/books/A Christmas Carol/page 5.png',
    'assets/books/A Christmas Carol/page 6.png',
    'assets/books/A Christmas Carol/page 7.png',
    'assets/books/A Christmas Carol/page 8.png',
    'assets/books/A Christmas Carol/page 9.png',
    'assets/books/A Christmas Carol/page 10.png',
    // Add more pages as needed
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageFlipWidget(
        key: _controller,
        backgroundColor: Colors.white,
        lastPage: Container(
            color: Colors.white, child: const Center(child: Text('The End!'))),
        children: [
          for (var imagePath in imagePaths) _buildDemoPage(imagePath),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.looks_one_outlined),
        onPressed: () {
          _controller.currentState?.goToPage(1);
        },
      ),
    );
  }

  Widget _buildDemoPage(String imagePath) {
    return Scaffold(
      body: Image.asset(
        imagePath,
        fit: BoxFit.cover,
      ),
    );
  }
}

推荐答案

您可以使用sharedPference来存储和检索LastLeftOverPageIndex. You can download the library from here

当用户退出应用程序时,必须保存当前页码,或者使用停用方法导航到不同的屏幕,并使用initState方法进入屏幕时恢复存储的先前存储的索引;

我已经修改了你的代码

class BookDemo extends StatefulWidget {
  const BookDemo({super.key});

  @override
  State<BookDemo> createState() => _BookDemoState();
}

class _BookDemoState extends State<BookDemo> {
  final _controller = GlobalKey<PageFlipWidgetState>();

  // sharedPreferences to store lastLeftOverPage Index
  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  // last left over page index
  int lastLeftOverPageIndex = 0;

  // SharedPreference key to retrieve the  value of lastLeftOverPageIndex
  String lastLeftOverPageNoPrefKey = "_lastLeftOverPageNoPrefKey";

  final List<String> imagesPath = [
    "assets/images/image_1.png",
    "assets/images/image_2.png",
    "assets/images/image_3.png"
  ];

  Widget _buildDemoPage(String imagePath) {
    return Scaffold(
      body: Image.asset(
        imagePath,
        fit: BoxFit.cover,
      ),
    );
  }

  @override
  void initState() {
    // restore the previous lastLeftOverPageIndex;
    _restoreLeftOverPage();
    super.initState();
  }

  Future<void> _restoreLeftOverPage() async {
    SharedPreferences pref = await _prefs;
    lastLeftOverPageIndex =
        pref.getInt(lastLeftOverPageNoPrefKey)?.toInt() ?? 0;
    // navigate the book page index to lastLeftOverPageIndex
    _controller.currentState?.goToPage(lastLeftOverPageIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageFlipWidget(
        key: _controller,
        backgroundColor: Colors.white,
        initialIndex: lastLeftOverPageIndex,
        lastPage: Container(
            color: Colors.white, child: const Center(child: Text('The End!'))),
        children: [
          for (var imagePath in imagesPath) _buildDemoPage(imagePath),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.looks_one_outlined),
        onPressed: () {
          _controller.currentState?.goToPage(1);
        },
      ),
    );
  }

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

  @override
  void deactivate() {
// before disposing the widget save the current page no
    var currentPageNo = _controller.currentState?.pageNumber.toInt() ?? 0;
    saveLastLeftOverPaqePref(currentPageNo);
  }

  Future<void> saveLastLeftOverPaqePref(var lastPage) async {
    SharedPreferences pref = await _prefs;
    pref.setInt(lastLeftOverPageNoPrefKey, lastPage);
  }
}

如果我的回答对你有效,别忘了做个记号.

Ios相关问答推荐

(Flutter)获取图像从图像拾取器保存和加载问题

如何遮罩,动画和添加阴影到CAShapeLayer?

与iPadOS中带有扣件的模式相似的组件是什么?

TrueDepth摄像机像素距离不准确

在TVOS中访问SWIFT中的objc++函数时发送给类的无法识别的 Select 符

当操作修改查看条件时,按钮不重复

如何防止UITest套件与Fastlane一起执行?

如何创建装饰视图?

iOS应用启动时崩溃问题解决方法-NO_CRASH_STACK-DYLD 4个符号丢失

无法在 CollectionView 中禁用部分弹跳 - 组合布局

在 SwiftUI 中重构提取到子视图的 GeometryReader 代码

叠加视图不在堆栈中维护自身 - SwiftUI

将值传递给导航链接视图中的文本框

SwiftUI 文本字段代理绑定代码忽略空格不起作用?

滑动以返回特定的 SwiftUI 视图

async/await、Task 和 [weak self]

使用 Vim 代替(或与)Xcode 进行 iOS 开发

如何阻止图像在 UIImageView 中拉伸?

以编程方式 Select UITextField 中的所有文本

SRCROOT 和 PROJECT_DIR 有什么区别?