Flutter - 我想使用 GestureDetector 而不是按钮
Flutter - I want to use GestureDetector rather than button
当前代码使用按钮生成屏幕截图,但我想使用 GestureDetector 而不是按钮生成屏幕截图。
如果在原来的代码里放GestureDetector,抓屏应该在最下面创建,但是并没有创建。如果您能帮助我用代码解释一下,我将不胜感激。
目前,只有按下黄色按钮时才会创建捕获,图像出现在底部,但我想在黑色容器中使用 GestureDetector 来产生与图像相同的结果。
我尝试用 GestureDetector 小部件包装并且
我得到了你期望的结果。
你能告诉我如何或在哪里使用 GestureDetector 吗?
import 'package:flutter/material.dart';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage_1(),
);
}
}
class MyHomePage_1 extends StatefulWidget {
@override
_MyHomePage_1State createState() => _MyHomePage_1State();
}
class _MyHomePage_1State extends State<MyHomePage_1> {
GlobalKey _globalKey = GlobalKey();
var _bytes;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Screenshot Example")),
body: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 250,
height: 250,
color: Colors.teal,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
onLongPress: () async {
final render = (_globalKey.currentContext
.findRenderObject() as RenderRepaintBoundary);
final imageBytes = (await (await render.toImage())
.toByteData(format: ImageByteFormat.png))
.buffer
.asUint8List();
setState(() {
_bytes = imageBytes;
});
},
child: RepaintBoundary(
key: _globalKey,
child: Container(
width: 100,
height: 100,
color: Colors.black87,
),
),
),
],
),
),
]),
/// display
if (_bytes != null) Image.memory(_bytes, width: 250),
],
),
);
}
}
当前代码使用按钮生成屏幕截图,但我想使用 GestureDetector 而不是按钮生成屏幕截图。
如果在原来的代码里放GestureDetector,抓屏应该在最下面创建,但是并没有创建。如果您能帮助我用代码解释一下,我将不胜感激。
目前,只有按下黄色按钮时才会创建捕获,图像出现在底部,但我想在黑色容器中使用 GestureDetector 来产生与图像相同的结果。
我尝试用 GestureDetector 小部件包装并且
我得到了你期望的结果。
你能告诉我如何或在哪里使用 GestureDetector 吗?
import 'package:flutter/material.dart';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage_1(),
);
}
}
class MyHomePage_1 extends StatefulWidget {
@override
_MyHomePage_1State createState() => _MyHomePage_1State();
}
class _MyHomePage_1State extends State<MyHomePage_1> {
GlobalKey _globalKey = GlobalKey();
var _bytes;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Screenshot Example")),
body: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 250,
height: 250,
color: Colors.teal,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
onLongPress: () async {
final render = (_globalKey.currentContext
.findRenderObject() as RenderRepaintBoundary);
final imageBytes = (await (await render.toImage())
.toByteData(format: ImageByteFormat.png))
.buffer
.asUint8List();
setState(() {
_bytes = imageBytes;
});
},
child: RepaintBoundary(
key: _globalKey,
child: Container(
width: 100,
height: 100,
color: Colors.black87,
),
),
),
],
),
),
]),
/// display
if (_bytes != null) Image.memory(_bytes, width: 250),
],
),
);
}
}