如何在Flutter中创建三次单击按钮 [英] How to create triple click button in flutter
问题描述
如何在flutter中实现三击按钮?
How can I implement a triple-click button in flutter?
三击时,该按钮会将条目存储在Firebase数据库中。
On triple-click, the button will store an entry in Firebase database.
推荐答案
可能无法实现三次单击按钮。但是,如果您真的想尽快使它工作,那么一种简单的方法可能是维护一个计数器以完成点击次数。一旦计数达到3,您需要将条目添加到Firestore。
Implementing "triple click" button in flutter might not be possible. But, if you really want to make it work ASAP then a simple method could be to maintain a counter for the number of clicks done. Once the count reaches 3, you need to add your entry to Firestore.
我已经从flutter的计数器应用样板中修改了代码。
希望您的pubspec.yaml文件中有cloud_firestore。如果没有,则将其添加,并将services.json也放入android的应用程序文件夹或ios的相应目录中。
I have modified the code from the counter app boilerplate of flutter.
Hope you have cloud_firestore in your pubspec.yaml file. If not then add it and put the services.json as well in the app folder of android or respective directory of ios.
cloud_firestore: ^0.13.4+1
所以,现在您可以看一下我的代码使用。
So, now you can have a look at the code that I am using.
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
_incrementCounter() {
setState(() {
_counter++;
});
if (_counter == 3) {
Firestore.instance
.collection('/sampleData')
.add({'data': "data"}).catchError((e) {
print(e);
});
setState(() {
_counter = 0;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
我已经编辑了_incrementCounter函数。我添加了一个条件语句来检查_counter是否为3。然后,我添加了Firestore条目。稍后,最重要的是将_counter设置为0,以便用户下次按下按钮3次时,代码将相应地工作。您可以根据需要自定义它。
I have edited the _incrementCounter function. I added a conditional statement to check the _counter if it is 3 or not. Then, I am adding the Firestore entry. Later on, the most important bit is to set the _counter as 0 so that the next time the user presses the button 3 times, then the code will work accordingly. You can customize it according to your needs.
但是请记住,尚未在颤动中发明三次单击,这只是一种变通解决方案,因此请勿将其用于现实生活开发应用程序,因为这将是一个非常糟糕的做法。
But remember, triple clicks have not been yet invented in flutter and this is just a work-around solution and don't use it for Real-life Development applications as this would be a very bad practice.
这篇关于如何在Flutter中创建三次单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!