flutter_other_plugin 0.0.2 flutter_other_plugin: ^0.0.2 copied to clipboard
A new Flutter plugin.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_other_plugin/flutter_other_plugin.dart';
import 'dart:ui' as ui;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Save image to gallery',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyHomePage> {
GlobalKey _globalKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Save image to gallery"),
),
body: Center(
child: Column(
children: <Widget>[
RepaintBoundary(
key: _globalKey,
child: Container(
width: 200,
height: 200,
color: Colors.red,
),
),
Container(
child: RaisedButton(
onPressed: _saved,
child: Text("保存到相册"),
),
width: 100,
height: 50,
)
],
),
));
}
_saved() async {
RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final result = await FlutterOtherPlugin.save(byteData.buffer.asUint8List());
print(result);
}
}