I have a project.
This project is need to have a feature that, when we clicked a button
the button will have a timer start, and then will go to another app. I have successfully done that, but the problem is. When the timer come off the another app should be back to my flutter app. I need help with this problem
Thank You.
This is my main_page.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:wartel_lapas/core/widget/ButtonCustom.dart';
import 'package:wartel_lapas/core/widget/PageCustom.dart';
import 'package:wartel_lapas/pages/token_input_page.dart';
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageCustom(
padding: const EdgeInsets.only(
top: 130,
right: 18,
left: 18,
),
children: [
SizedBox(
height: 200,
child: Image.asset(
'assets/deltakode.png',
fit: BoxFit.fill,
),
),
const SizedBox(
height: 30,
),
Text(
'Selamat datang!',
softWrap: true,
overflow: TextOverflow.clip,
textAlign: TextAlign.center,
style: GoogleFonts.plusJakartaSans(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 15,
),
Text(
'Di Aplikasi Wartel Lapas Silahkan nKlik Tombol di Bawah Ini Untuk nMemulai',
softWrap: true,
overflow: TextOverflow.clip,
textAlign: TextAlign.center,
style: GoogleFonts.plusJakartaSans(
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
const SizedBox(
height: 210,
),
ButtonCustom(
placeholder: 'Mulai!',
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const TokenInputPage(),
),
);
},
),
],
),
);
}
}
This is my token_input_dart.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:wartel_lapas/core/widget/ButtonCustom.dart';
import 'package:wartel_lapas/core/widget/PageCustom.dart';
import 'package:wartel_lapas/pages/phone_input_page.dart';
class TokenInputPage extends StatefulWidget {
const TokenInputPage({super.key});
@override
State<TokenInputPage> createState() => _TokenInputPageState();
}
class _TokenInputPageState extends State<TokenInputPage> {
final _token = TextEditingController();
int timeLeft = 5;
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageCustom(
padding: const EdgeInsets.only(top: 130, right: 16, left: 16),
children: [
SizedBox(
height: 200,
child: Image.asset(
'assets/deltakode.png',
fit: BoxFit.fill,
),
),
const SizedBox(
height: 30,
),
Text(
'Masukan Voucher Anda!',
softWrap: true,
overflow: TextOverflow.clip,
textAlign: TextAlign.center,
style: GoogleFonts.plusJakartaSans(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 50,
),
TextField(
controller: _token,
decoration: InputDecoration(
labelText: 'Masukan Voucher',
alignLabelWithHint: true,
labelStyle: GoogleFonts.plusJakartaSans(fontSize: 16)),
keyboardType: TextInputType.text,
),
const SizedBox(
height: 20,
),
ButtonCustom(
placeholder: 'Masuk',
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PhoneInputPage(
timeLeft: timeLeft,
),
),
);
},
)
],
),
);
}
}
And this is my phone_input_page.dart
import 'dart:async';
// import 'package:android_intent/android_intent.dart';
import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:wartel_lapas/core/widget/ButtonCustom.dart';
import 'package:wartel_lapas/core/widget/PageCustom.dart';
class PhoneInputPage extends StatefulWidget {
const PhoneInputPage({super.key, this.timeLeft});
final timeLeft; // Ensure timeLeft is passed as an integer (in seconds)
@override
State<PhoneInputPage> createState() => _PhoneInputPageState();
}
class _PhoneInputPageState extends State<PhoneInputPage> {
final _phoneController = TextEditingController();
bool isLoading = false;
int? balanceAmount;
String? transactionId;
Timer? _timer;
late int _remainingTime; // Countdown timer value
@override
void dispose() {
_phoneController.dispose();
_timer?.cancel();
_closeVideoCall();
super.dispose();
}
void _startCountdown() {
_remainingTime = widget.timeLeft; // Initialize with the provided time
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
if (_remainingTime > 0) {
_remainingTime--;
} else {
_timer?.cancel();
_closeVideoCall();
Navigator.pop(context);
}
});
});
}
void _closeVideoCall() async {
print('Waktu Abis');
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Panggilan telah diakhiri.')),
);
// Bring the app back to the foreground
// const intent = AndroidIntent(
// action: 'action_view',
// data: 'package:com.example.wartel_lapas',
// package: 'com.example.wartel_lapas',
// );
// await intent.launch();
print('Balik');
}
String normalizePhoneNumber(String phone) {
phone = phone.replaceAll(RegExp(r'D'), '');
if (phone.startsWith('0')) {
phone = '62${phone.substring(1)}';
}
if (!phone.startsWith('62')) {
return ''; // Invalid format
}
return phone;
}
void _startVideoCall() async {
setState(() {
isLoading = true;
});
String normalizedPhone = normalizePhoneNumber(_phoneController.text);
if (normalizedPhone.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Kode negara tidak ada atau salah.')),
);
setState(() {
isLoading = false;
});
return;
}
_startCountdown();
final intent = AndroidIntent(
action: 'action_view',
data: 'https://wa.me/$normalizedPhone',
package: 'com.whatsapp',
);
await intent.launch();
setState(() {
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageCustom(
padding: const EdgeInsets.only(
top: 130,
right: 18,
left: 18,
),
children: [
SizedBox(
height: 200,
child: Image.asset(
'assets/deltakode.png',
fit: BoxFit.fill,
),
),
const SizedBox(
height: 30,
),
Text(
'Masukan Nomor Telepon Yang Dituju!',
softWrap: true,
overflow: TextOverflow.clip,
textAlign: TextAlign.center,
style: GoogleFonts.plusJakartaSans(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 30,
),
Text(
_timer == null
? 'Waktu Anda Tersedia: ${widget.timeLeft.toString()} detik'
: 'Waktu Anda Sisa: $_remainingTime detik',
style: GoogleFonts.plusJakartaSans(
fontSize: 16,
fontWeight: FontWeight.w700,
),
),
TextField(
controller: _phoneController,
decoration: const InputDecoration(
labelText: 'Nomor Telepon',
),
keyboardType: TextInputType.phone,
),
const SizedBox(
height: 20,
),
ButtonCustom(
onPressed: isLoading ? null : _startVideoCall,
placeholder:
isLoading ? const CircularProgressIndicator() : 'Mulai Chat',
),
],
),
);
}
}
My package that im using is
cupertino_icons: ^1.0.6
google_fonts: ^6.2.1
android_intent_plus: ^5.2.0
How to get the second app back to my flutter app
Thank you
Albani Zaidan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2