Я использую Android Intent во Flutter, а затем хочу, чтобы приложение вернулось в мое приложение, как это сделать?Android

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Я использую Android Intent во Flutter, а затем хочу, чтобы приложение вернулось в мое приложение, как это сделать?

Сообщение Anonymous »

У меня есть проект.
В этом проекте должна быть функция, которая при нажатии кнопки
у кнопки будет запускаться таймер, а затем она будет работать. в другое приложение. Я успешно это сделал, но проблема в том. Когда таймер сработает, другое приложение должно вернуться в мое приложение Flutter. Мне нужна помощь с этой проблемой.
Спасибо.
Это моя главная_страница.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 createState() => _MainPageState();
}

class _MainPageState extends State {
@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(),
),
);
},
),
],
),
);
}
}

Это мой токен_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 createState() => _TokenInputPageState();
}

class _TokenInputPageState extends State {
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,
),
),
);
},
)
],
),
);
}
}

А это мой телефон_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
 createState() => _PhoneInputPageState();
}

class _PhoneInputPageState extends State {
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',
),
],
),
);
}
}
Я использую пакет
cupertino_icons: ^1.0.6
google_fonts: ^6.2.1
android_intent_plus: ^5.2.0
Как вернуть второе приложение в мое приложение Flutter
Спасибо

Подробнее здесь: https://stackoverflow.com/questions/792 ... -to-my-app
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»