dart 如何在Flutter中保存绘制直线的路径

w41d8nur  于 2023-11-14  发布在  Flutter
关注(0)|答案(1)|浏览(134)

我做了一个屏幕,用户可以使用image_painter在图像周围绘制,它工作得很好。现在我希望能够保存用户绘制的线的路径,但无论我如何搜索,我什么也找不到。
我怎么能这么做?我还尝试添加手势,但没有效果,这样的库没有保存路径的选项,这对我来说似乎很奇怪

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

class DrawingScreen extends StatefulWidget {
  @override
  _DrawingScreenState createState() => _DrawingScreenState();
}

class _DrawingScreenState extends State<DrawingScreen> {
  final _imageKey = GlobalKey<ImagePainterState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.only(top: 50.0),
        child: Center(
          child: Column(
            children: [
              SizedBox(
                height: 600,
                child: ImagePainter.network(
                  "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ-MhSib_BLGD1SYgEQpXUmmQy0TfyJjEbfDA",
                  key: _imageKey,
                  colors: const [Colors.red, Colors.blue],
                  scalable: true,
                  initialPaintMode: PaintMode.freeStyle,
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 20.0),
                child: ElevatedButton(
                  onPressed: () {
                    // Save or process the user's drawing here
                  },
                  style: ElevatedButton.styleFrom(
                    foregroundColor: Colors.white,
                    backgroundColor: Colors.green,
                    padding: const EdgeInsets.symmetric(
                      horizontal: 20,
                      vertical: 12,
                    ),
                    textStyle: const TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                  child: const Text('Submit'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

字符串

7rtdyuoh

7rtdyuoh1#

流这个例子。

import 'dart:async';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_painter/image_painter.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DrawingScreen(),
    );
  }
}

class DrawingScreen extends StatefulWidget {
  @override
  _DrawingScreenState createState() => _DrawingScreenState();
}

class _DrawingScreenState extends State<DrawingScreen> {
  final _imageKey = GlobalKey<ImagePainterState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.only(top: 50.0),
        child: Center(
          child: Column(
            children: [
              SizedBox(
                height: 600,
                child: ImagePainter.network(
                  "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ-MhSib_BLGD1SYgEQpXUmmQy0TfyJjEbfDA",
                  key: _imageKey,
                  colors: const [Colors.red, Colors.blue],
                  scalable: true,
                  initialPaintMode: PaintMode.freeStyle,
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 20.0),
                child: ElevatedButton(
                  onPressed: () async {
                    Uint8List? byteArray =
                    await _imageKey.currentState?.exportImage();

                    if (byteArray != null) {
                      final result =
                      await imagegallerysaver.savefile(byteArray);
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    foregroundColor: Colors.white,
                    backgroundColor: Colors.green,
                    padding: const EdgeInsets.symmetric(
                      horizontal: 20,
                      vertical: 12,
                    ),
                    textStyle: const TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                  child: const Text('Submit'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

字符串

新增保存文件方式

相关问题