How does Flutter use Completer for anti-shake
In Flutter, Completer can be used to implement anti-shake functionality. Buffeting is used to ensure that all triggers within the time are combined into a single request. For successive event triggers (such as keyboard input by the user, successive button clicks), the actual action is performed only if the event is not triggered again within the specified delay time.
Here is an example of how to implement asynchronous shake prevention using Completer, as follows:
import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Drag to Sort', home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final List<String> _items = List<String>.generate(10, (i) => 'Item $i'); bool _isReorderable = false; @override void initState() { // TODO: implement initState super.initState(); var debouncer = Debouncer(delay: Duration(seconds: 1)); debouncer.run(() => print('Action 1')); debouncer.run(() => print('Action 2')); debouncer.run(() => print('Action 3')); Future.delayed(Duration(seconds: 2), () {debouncer.run(() => print('Action after delay')); // Delayed (duration (seconds: 2), () {debouncer.run(() => print(' action after delay')); }); } @override Widget build(BuildContext context) { return Scaffold( // backgroundColor: Colors.blueAccent, appBar: AppBar( title: Text('Test'), ), body: Column(children: [ _buildContainer(Colors.lightBlue,const Flexible( child: Text(" This is a project ",maxLines: 1,overflow: TextOverflow.ellipsis,))), _buildContainer(Colors.red, const Flexible(fit: FlexFit.tight, child: Text(" This is a project ",maxLines: 1,overflow: TextOverflow.ellipsis,))), _buildContainer(Colors.purple, Flexible( fit: FlexFit.tight, child: Text(" This is a project "* 6,maxLines: 1,overflow: TextOverflow.ellipsis,)), _buildContainer(Colors.blue, Expanded(child: Text(" This is a project "* 6,maxLines: 1,overflow: TextOverflow.ellipsis,))),],); } Container _buildContainer(Color color,Widget child) { return Container( height: 56, color: color, child: Row(children: [const SizedBox(width:16), const Text(" Source: "), child, const SizedBox(width: 8), Container( padding: EdgeInsets.all(5), decoration: const BoxDecoration( color: Colors.cyan, borderRadius: BorderRadius.all(Radius.circular(6)) ), child: The Text (" project "),), const SizedBox (width: 16),],),); } } class Debouncer { final Duration delay; Completer? _lastCompleter; Timer? _timer; Debouncer({required this.delay}); void run(Function action) {// Cancel if (_lastCompleter!) = null && ! _lastCompleter! .isCompleted) { _lastCompleter! .completeError('Cancelled'); } _lastCompleter = Completer(); // Reset the timer? .cancel(); _timer = Timer(delay, () { action(); _lastCompleter! .complete(); }); // Handle the cancel operation _lastCompleter! .future.catchError((error) {print(' Operation cancelled '); }); }}
Print as follows:
In this example:
Debouncer
Class contains anti-shake logic.run
Method accepts an action to be executed and ensures that in successive calls, only the last call will be executed after a specified delay.- when
run
When a method is called continuously, it passesCompleter
Cancel the previous unfinished action and restart the timer. - The last action is performed only when the delay time has passed and there are no new calls.
This approach effectively limits how often events (such as user input, button clicks, etc.) are processed, thereby optimizing performance and resource utilization. In practical applications, you may need to adjust the delay time and processing logic according to the specific situation.
This is the end of the article on how Flutter uses the Completer to implement the Flutter Flutter Flutter anti-shake feature, more Flutter Completer anti-shake content please search for previous articles of Script House or continue to browse the related articles below hope that you will support Script House in the future!
Related article
-
OpenGL ES achieves lighting effects (6)
This article mainly introduces OpenGL ES to achieve lighting effect in detail, which has certain reference value, interested partners can refer to it2018-05-05 -
Android camera2 Example of determining whether the camera function is controllable
The following Xiaobian will bring you an example of Android camera2 judging whether the camera function is controllable. Xiaobian feel very good, now to share with you, but also to give you a reference. Let's take a look2017-03-03 -
An example explores the life cycle of Android fragments when writing applications
This article mainly introduces the exploration of the life cycle of the Fragment when the Android application is written. The control of the three states of resumed, paused and stoped needs to be mastered, and friends in need can refer to it2016-02-02 -
30 android project development tips and experiences
This article summarizes 30 android project development skills and experience for everyone, and friends in need can refer to it2018-04-04 -
Android mimics Tiktok sliding up and down layout
This article mainly introduces the Android imitation Tiktok sliding layout in detail, the example code in the article is very detailed, has a certain reference value, interested partners can refer to it2019-12-12 -
RadioGroup implements multiple rows of radio boxes
This article mainly introduces in detail the multi-line arrangement of RadioGroup to achieve the radio Group, the example code in the article is very detailed, has a certain reference value, interested partners can refer to it2019-11-11 -
TextView and ImageView implement skew effects in Android
This article mainly introduces in detail the Android TextView and ImageView to achieve tilt effect, has a certain reference value, interested partners can refer to it2017-08-08 -
Analysis of Android mobile phone guard sim card binding
This article mainly introduces the analysis of Android mobile phone guard sim card binding related information, need friends can refer to2016-04-04 -
This article mainly introduces the information about the use of butterknife in Android and the automatic search component plug-in, the article introduces very detailed through the example code, which has a certain reference value for you Android developers, and the friends who need to learn together2019-10-10
-
Example analysis of Android programming monitor usage
This article mainly introduces the use of Android programming monitor, combined with examples of more detailed analysis of the function of Android monitor and SMS monitoring and response operation skills, need friends can refer to the next2016-01-01
Latest comments