flutter實(shí)現(xiàn)點(diǎn)擊事件
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)點(diǎn)擊事件的具體代碼,供大家參考,具體內(nèi)容如下
在Android中,您可以通過(guò)調(diào)用方法setOnClickListener將OnClick綁定到按鈕等view上。
在Flutter中,有兩種方法:
1.如果Widget支持事件監(jiān)聽(tīng),則可以將一個(gè)函數(shù)傳遞給它并進(jìn)行處理。例如,RaisedButton有一個(gè)onPressed參數(shù)
@override
Widget build(BuildContext context) {
return new RaisedButton(
onPressed: () {
print("click");
},
child: new Text("Button"));
}
2.如果Widget不支持事件監(jiān)聽(tīng),則可以將該Widget包裝到GestureDetector中,并將處理函數(shù)傳遞給onTap參數(shù)
class SampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new GestureDetector(
child: new FlutterLogo(
size: 200.0,
),
onTap: () {
print("tap");
},
),
));
}
}
2.1.使用GestureDetector,可以監(jiān)聽(tīng)多種手勢(shì)
(1)Tap
onTapDown
onTapUp
onTap
onTapCancel
(2)Double tap
onDoubleTap 用戶(hù)快速連續(xù)兩次在同一位置輕敲屏幕
(3)長(zhǎng)按
onLongPress
(4)垂直拖動(dòng)
onVerticalDragStart
onVerticalDragUpdate
onVerticalDragEnd
(5)水平拖拽
onHorizontalDragStart
onHorizontalDragUpdate
onHorizontalDragEnd
2.2.示例:監(jiān)聽(tīng)FlutterLogo的雙擊事件,雙擊時(shí)使其旋轉(zhuǎn)。
void main() => runApp(DemoApp());
class DemoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: '導(dǎo)航演示1',
home: new MyAppHome(),
);
}
}
class MyAppHome extends StatefulWidget{
@override
_MyAppHomeState createState() => _MyAppHomeState();
}
class _MyAppHomeState extends State<MyAppHome> with TickerProviderStateMixin{
AnimationController controller;
CurvedAnimation curve;
@override
void initState() {
super.initState();
controller = new AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
curve = new CurvedAnimation(parent: controller, curve: Curves.easeIn);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new GestureDetector(
child: new RotationTransition(
turns: curve,
child: new FlutterLogo(
size: 200.0,
)),
onDoubleTap: () {
if (controller.isCompleted) {
controller.reverse();
} else {
controller.forward();
}
},
),
));
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android打開(kāi)相機(jī)和相冊(cè)實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了Android打開(kāi)相機(jī)和相冊(cè)實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
android仿愛(ài)奇藝加載動(dòng)畫(huà)實(shí)例
這篇文章主要介紹了android仿愛(ài)奇藝加載動(dòng)畫(huà)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。2016-10-10
Android實(shí)現(xiàn)數(shù)據(jù)按照時(shí)間排序
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)數(shù)據(jù)按照時(shí)間排序的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設(shè)計(jì)詳解及實(shí)例
這篇文章主要介紹了房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設(shè)計(jì)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android編程實(shí)現(xiàn)自定義分享列表ACTION_SEND功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義分享列表ACTION_SEND功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了自定義分享列表功能的步驟與具體操作技巧,需要的朋友可以參考下2017-02-02
Kotlin面向?qū)ο笾R(shí)點(diǎn)講解
面向?qū)ο缶幊掏ㄟ^(guò)對(duì)事物的抽象,大大的簡(jiǎn)化了程序的開(kāi)發(fā)難度。我們常用的編程語(yǔ)言:Java、C++、Python都屬于面向?qū)ο缶幊?。Kotlin與java類(lèi)似,也是一種面向?qū)ο缶幊陶Z(yǔ)言。本文從面向?qū)ο笕齻€(gè)基本特征:封裝、繼承、多態(tài),來(lái)闡述一下Kotlin中的面向?qū)ο缶幊?/div> 2022-12-12
Android關(guān)鍵字persistent詳細(xì)分析
這篇文章主要介紹了Android關(guān)鍵字persistent的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04最新評(píng)論

