flutter 如何使用共享首选项将画布drawPath保存到设备

jpfvwuh4  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(150)

我正在制作一个电子书应用程序,用户可以在堆叠在Pdfviewer上的Container小工具上绘制线条,我希望通过使用共享首选项或其他方式将绘制的线条保存到用户的设备上。
这是我代码

return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 1,
      ),
      body: Stack(
        children: [
          InteractiveViewer(
            panEnabled: _memoMode ? false : true,
            scaleEnabled: _memoMode ? false : true,
            maxScale: 3,
            child: Stack(
              children: [
                IgnorePointer(
                  ignoring: true,
                  child: SfPdfViewer.network(
                    widget.downloadedURL,
                    controller: _pdfViewerController,
                    key: _pdfViewerKey,
                    pageLayoutMode: PdfPageLayoutMode.single,
                    enableDoubleTapZooming: false,
                    // Save the last closed page number
                    onPageChanged: (details) {
                      _lastClosedPage = details.newPageNumber;
                      _currentPage = details.newPageNumber;
                      countLastClosedPage();
                    },
                    onDocumentLoaded: (details) {
                      // PDF Open 하면 memo page와 문제집 page를 matching
                      _pdfViewerController.jumpToPage(_lastClosedPage);
                      _pdfViewerController.zoomLevel = 0;
                      print("totalpages = ${_countTotalPages().toInt()}");

                    },
                    canShowScrollHead: false,
                  ),
                ),
               // User can draw lines on the WorkbookDrawingPage widget 
                SingleChildScrollView(
                  physics: NeverScrollableScrollPhysics(),
                  controller: _scrollController,
                  scrollDirection: Axis.horizontal,
                  child: WorkbookDrawingPage(widget.workbookName, _countTotalPages().toInt()),
                ),

WorkbookDawingPage.dart

return Container(
      width: MediaQuery.of(context).size.width * (widget.countTotalPages),
      height: MediaQuery.of(context).size.height * 1,
      child: CustomPaint(
        painter: DrawingPainter(p.lines, widget.workbookName),
        child: Listener(
         ...
         ...

DrawingPainter.dart

class DrawingPainter extends CustomPainter {

  final List<List<DotInfo>> lines;
  String workbookName;

  DrawingPainter(this.lines, this.workbookName);

  @override
  void paint(Canvas canvas, Size size) async {

      for (var oneLine in lines) {
        Color? color;
        double? size;
        var path = Path();
        var l = <Offset>[];
        for (var oneDot in oneLine) {
          color ??= oneDot.color;
          size ??= oneDot.size;
          l.add(oneDot.offset);
        }
        path.addPolygon(l, false);
        canvas.drawPath(
            path,
            Paint()
              ..color = color!
              ..strokeWidth = size!
              ..strokeCap = StrokeCap.round
              ..style = PaintingStyle.stroke
              ..isAntiAlias = true
              ..strokeJoin = StrokeJoin.round);

      }

  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}

有什么方法可以保存绘制的线条吗?

3duebb1j

3duebb1j1#

shared_preferences库所述,您只能直接存储某些数据类型:Supported data types are int, double, bool, String and List<String>.
你可以把DotInfo转换成JSON,然后把它作为字符串化版本存储在存储器中。如果你需要以后提取数据,你可以把字符串化版本解码回JSON,并按预期显示它。
您可以查看this question以了解如何执行此操作!

相关问题