flutter 手势

2022-05-19  本文已影响0人  feitry

先放代码:

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  final String title;

  const HomePage({super.key, required this.title});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text("hi,${widget.title}"),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'web title',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyButton(), //const HomePage(title: 'title'),
    );
  }
}

void main(List<String> args) {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // ignore: avoid_print
        print('object:on tap');
      },
      onDoubleTap: () {
        // ignore: avoid_print
        print('object:double tap');
      },
      onLongPress: () {
        // ignore: avoid_print
        print('object:on long press');
      },
      child: const HomePage(title: 'title-tap'),
    );
  }
}

运行效果:


Simulator Screen Shot - iPhone 13 Pro Max - 2022-05-19 at 16.33.44.png

在页面可进行轻点、双击、长安,控制台打印相应的调用方法:


截屏2022-05-19 下午4.35.51.png

HomePage也可以是其他Widget ,实现手势效果,GestureDetector,在gesture_detector.dart文件中可以看到,它支持各种手势:


截屏2022-05-19 下午4.31.13.png
上一篇下一篇

猜你喜欢

热点阅读