27.3 手势识别(缩放)

1.源代码
import 'package:flutter/material.dart';

void main () => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  double imgWidth = 200.0;
  double imgHeight = 200.0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(title: Text("手势检测"),),
          body: Center(
              child: GestureDetector(
                child: Image.network("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3669987090,2161151684&fm=26&gp=0.jpg", width: imgWidth,height: imgHeight,),
                onScaleUpdate: (ScaleUpdateDetails e) {
                  setState(() {
                    imgWidth= 200 * e.scale.clamp(0.8, 10.0);
                    imgHeight= 200 * e.scale.clamp(0.8, 10.0);
                  });
                },
              )
          ),
        )
    );
  }
}
2.解释源代码
import 'package:flutter/material.dart';

void main () => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  //设置起始图片高宽度大小
  double imgWidth = 200.0;
  double imgHeight = 200.0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(title: Text("手势检测"),),
          body: Center(
              child: GestureDetector(
                child: Image.network("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3669987090,2161151684&fm=26&gp=0.jpg", width: imgWidth,height: imgHeight,),
                onScaleUpdate: (ScaleUpdateDetails e) {
                  setState(() {
                    //缩放倍数在0.8到10倍之间
                    imgWidth= 200 * e.scale.clamp(0.8, 10.0);
                    imgHeight= 200 * e.scale.clamp(0.8, 10.0);
                  });
                },
              )
          ),
        )
    );
  }
}
3.效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐