【问题标题】:Screenshot in flutter webFlutter 网页中的截图
【发布时间】:2021-07-29 12:16:57
【问题描述】:

我想知道如何使用 flutter web 截取小部件。我已经尝试过名为 screenshot 的包,但它似乎无法在网络上运行。 有人知道我该怎么做吗?

【问题讨论】:

    标签: flutter screenshot flutter-web


    【解决方案1】:

    你试过了吗?

    import 'dart:typed_data';
    
    import 'package:flutter/material.dart';
    import 'package:screenshot/screenshot.dart';
    // import 'package:webview_flutter/webview_flutter.dart';
    // import 'package:image_gallery_saver/image_gallery_saver.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            textTheme: TextTheme(
                headline6: TextStyle(
              color: Colors.yellow,
              fontSize: 50,
            )),
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Screenshot Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key? key, required this.title}) : super(key: key);
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      //Create an instance of ScreenshotController
      ScreenshotController screenshotController = ScreenshotController();
    
      @override
      void initState() {
        // if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Screenshot(
                  controller: screenshotController,
                  child: Container(
                      padding: const EdgeInsets.all(30.0),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.blueAccent, width: 5.0),
                        color: Colors.amberAccent,
                      ),
                      child: Text("This widget will be captured as an image")),
                ),
                SizedBox(
                  height: 25,
                ),
                ElevatedButton(
                  child: Text(
                    'Capture Above Widget',
                  ),
                  onPressed: () {
                    screenshotController
                        .capture(delay: Duration(milliseconds: 10))
                        .then((capturedImage) async {
                      ShowCapturedWidget(context, capturedImage!);
                    }).catchError((onError) {
                      print(onError);
                    });
                  },
                ),
                ElevatedButton(
                  child: Text(
                    'Capture An Invisible Widget',
                  ),
                  onPressed: () {
                    var container = Container(
                        padding: const EdgeInsets.all(30.0),
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.blueAccent, width: 5.0),
                          color: Colors.redAccent,
                        ),
                        child: Text(
                          "This is an invisible widget",
                          style: Theme.of(context).textTheme.headline6,
                        ));
                    screenshotController
                        .captureFromWidget(
                            InheritedTheme.captureAll(
                                context, Material(child: container)),
                            delay: Duration(seconds: 1))
                        .then((capturedImage) {
                      ShowCapturedWidget(context, capturedImage);
                    });
                  },
                ),
              ],
            ),
          ),
        );
      }
    
      Future<dynamic> ShowCapturedWidget(
          BuildContext context, Uint8List capturedImage) {
        return showDialog(
          useSafeArea: false,
          context: context,
          builder: (context) => Scaffold(
            appBar: AppBar(
              title: Text("Captured widget screenshot"),
            ),
            body: Center(
                child: capturedImage != null
                    ? Image.memory(capturedImage)
                    : Container()),
          ),
        );
      }
    
      // _saved(File image) async {
      //   // final result = await ImageGallerySaver.save(image.readAsBytesSync());
      //   print("File Saved to Gallery");
      // }
    }
    

    别忘了运行flutter pub add screenshot

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 2022-10-14
      • 1970-01-01
      • 2019-08-09
      • 2013-06-15
      • 1970-01-01
      • 2010-09-08
      • 2011-10-25
      相关资源
      最近更新 更多