Skip to content

Instantly share code, notes, and snippets.

@marifdev
Last active April 16, 2023 13:24
Show Gist options
  • Save marifdev/e809e40bbb87a571bcf4672434023f4d to your computer and use it in GitHub Desktop.
Save marifdev/e809e40bbb87a571bcf4672434023f4d to your computer and use it in GitHub Desktop.
Step 4
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'home_controller.dart';
class MyHomePage extends StatelessWidget {
MyHomePage({super.key});
final MyHomePageController controller = Get.put(MyHomePageController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Voltran Assignment'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Obx(() => Text(
'${controller.tapCount}',
style: Theme.of(context).textTheme.headlineMedium,
)),
],
),
),
floatingActionButton: GestureDetector(
onLongPress: controller.onLongPress,
child: Obx(
() => FloatingActionButton(
onPressed: controller.onTap,
backgroundColor: controller.isIncreasing.value ? Colors.green : Colors.blue,
child: const Icon(Icons.add),
),
),
),
);
}
}
import 'package:get/get.dart';
class MyHomePageController extends GetxController {
List<Duration> tapDurations = [];
List<Duration> newDurations = [];
var tapCount = 0.obs;
DateTime? lastTapTime;
var isIncreasing = false.obs;
void onTap() {
final currentTime = DateTime.now();
final duration = lastTapTime != null ? currentTime.difference(lastTapTime!) : Duration.zero;
lastTapTime = currentTime;
if (isIncreasing.value) {
newDurations.add(duration);
} else {
tapDurations.add(duration);
}
}
Future<void> onLongPress() async {
isIncreasing.value = true;
for (var duration in tapDurations) {
await Future.delayed(duration);
tapCount++;
}
if (newDurations.isNotEmpty) {
tapDurations = List.from(newDurations);
newDurations.clear();
await onLongPress();
}
tapDurations = [];
lastTapTime = null;
isIncreasing.value = false;
}
}
import 'package:flutter/material.dart';
import 'pages/home/home.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Voltran Assignment',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
@marifdev
Copy link
Author

marifdev commented Apr 16, 2023

The technical assignment is as follows:

1- Change the default flutter app (flutter create myapp) to record user taps on FAB, but not incrementing the number right after each tabs. By long pressing the FAB, number starts to increment with the exact timing user tapped the FAB. There should be no limitation for number of taps and different sequence of delay between the taps. So in another word you should record user tap count and the time between each tab, and increment the number with the same delays on the screen just after long press happened on the button. Somehow, it simulates the user actions and we can run it later.
For example: I tap on the button 3 times so fast, I wait 1 second and I tap 2 times fast again, now the number is still zero on the screen. I will long press the button and the number goes to 3 so fast, it waits 1 second and it reaches 5 afterward.
2- Extend the implementation to make sure you can record new taps during execution(after you long press and number is changing)
3- Change the color of FAB to demonstrate when it is executing the actions(Custom colors)
4- Convert the current stateful widget to an stateless widget and implement the features by the help of GetxController

Each revision is for a step

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment