flutter實(shí)現(xiàn)點(diǎn)擊事件
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)點(diǎn)擊事件的具體代碼,供大家參考,具體內(nèi)容如下
在Android中,您可以通過調(diào)用方法setOnClickListener將OnClick綁定到按鈕等view上。
在Flutter中,有兩種方法:
1.如果Widget支持事件監(jiān)聽,則可以將一個(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)聽,則可以將該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)聽多種手勢(shì)
(1)Tap
onTapDown
onTapUp
onTap
onTapCancel
(2)Double tap
onDoubleTap 用戶快速連續(xù)兩次在同一位置輕敲屏幕
(3)長(zhǎng)按
onLongPress
(4)垂直拖動(dòng)
onVerticalDragStart
onVerticalDragUpdate
onVerticalDragEnd
(5)水平拖拽
onHorizontalDragStart
onHorizontalDragUpdate
onHorizontalDragEnd
2.2.示例:監(jiān)聽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實(shí)現(xiàn)簡(jiǎn)單的下拉阻尼效應(yīng)示例代碼
下面小編就為大家分享一篇Android實(shí)現(xiàn)簡(jiǎn)單的下拉阻尼效應(yīng)示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
android studio 一直卡在Gradle:Build Running的幾種解決辦法
這篇文章主要介紹了android studio 一直卡在Gradle:Build Running的解決辦法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
Android Zipalign工具優(yōu)化Android APK應(yīng)用
本文主要介紹Android Zipalign工具優(yōu)化Android APK應(yīng)用,這里整理了相關(guān)資料及簡(jiǎn)單優(yōu)化實(shí)例,有需要的小伙伴可以參考下2016-09-09
Android CheckBox中設(shè)置padding無效解決辦法
這篇文章主要介紹了Android CheckBox中設(shè)置padding無效解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家解決這樣類似的問題,需要的朋友可以參考下2017-10-10
WorkManager解決應(yīng)用退出后繼續(xù)運(yùn)行后臺(tái)任務(wù)
這篇文章主要為大家介紹了WorkManager解決應(yīng)用退出后繼續(xù)運(yùn)行后臺(tái)任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Android數(shù)據(jù)存儲(chǔ)幾種方式講解
在開發(fā)過程中,數(shù)據(jù)存取是較為頻繁的,今天我們來了解下android幾種常見的數(shù)據(jù)存取方式。在Android中,sharePreferences是一種輕量級(jí)的數(shù)據(jù)存儲(chǔ)方式,采用鍵值對(duì)的存儲(chǔ)方式,存儲(chǔ)少量數(shù)據(jù),支持基本類型的簡(jiǎn)單數(shù)據(jù)存儲(chǔ)2022-12-12
一文帶你了解Android?Flutter中Transform的使用
flutter的強(qiáng)大之處在于,可以對(duì)所有的widget進(jìn)行Transform,因此可以做出非??犰诺男Ч1疚木蛠泶蠹伊私庖幌耇ransform的具體使用,感興趣的可以了解一下2023-01-01
Android實(shí)戰(zhàn)教程第三篇之簡(jiǎn)單實(shí)現(xiàn)撥打電話功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第三篇之簡(jiǎn)單實(shí)現(xiàn)撥打電話功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

