首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从deep_link打开颤振应用程序所需的屏幕?

如何从deep_link打开颤振应用程序所需的屏幕?
EN

Stack Overflow用户
提问于 2022-03-16 11:03:48
回答 1查看 138关注 0票数 0

我使用的是uni_links包,它创建了一个deep_link,单击它会打开我需要的屏幕。现在我可以自动打开应用程序从给定的链接,但它没有到我需要的确切屏幕,它只是进入默认页面。如果这有帮助的话,我也会使用getX进行状态管理。如果问题不正确,请随意推荐。

需要从链接new_password_page.dart打开页面

代码语言:javascript
复制
import 'package:darcy/app/global_widgets/base_button.dart';
import 'package:darcy/app/global_widgets/outline_textfield.dart';
import 'package:darcy/app/modules/forgot_password/forgot_controller_page.dart';
import 'package:darcy/routes/pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:uni_links/uni_links.dart';

import '../../../core/theme/app_theme.dart';

class NewPasswordPage extends StatelessWidget {
  final ForgotController _homeController = Get.find<ForgotController>();
  NewPasswordPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String newPassword = '';
    String confirmPassword = '';
    return Scaffold(
      backgroundColor: AppTheme.base_background,
      appBar: AppBar(
        title: Text('New Password'),
        elevation: 0.0,
        backgroundColor: AppTheme.transparent,
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                SizedBox(
                  height: MediaQuery.of(context).size.height * 0.2,
                ),
                OutlinedTextField(
                  text: newPassword,
                  labelText: 'New password',
                  messageText: '',
                  onTextChange: (value) {
                    newPassword = value;
                  },
                  customIcon: Icons.password_rounded,
                  obscureText: true,
                ),
                const SizedBox(
                  height: 16.0,
                ),
                OutlinedTextField(
                  text: confirmPassword,
                  labelText: 'Confirm password',
                  messageText: '',
                  onTextChange: (value) {
                    confirmPassword = value;
                  },
                  customIcon: Icons.password_rounded,
                  obscureText: true,
                ),
                const SizedBox(
                  height: 32.0,
                ),
                SizedBox(
                    height: 40.0,
                    width: double.infinity,
                    child: BaseButton(
                      buttonText: 'confirm'.tr,
                      buttonColor: AppTheme.sign_in,
                      buttonFunction: () {
                        Get.toNamed(Routes.SUCCESS);
                      },
                    )),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

new_password_controller.dart

代码语言:javascript
复制
import 'dart:async';

import 'package:darcy/app/data/network/services/user_api.dart';
import 'package:darcy/core/base_controller/base_controller.dart';
import 'package:darcy/routes/pages.dart';
import 'package:flutter/services.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';
import 'package:get/get.dart';
import 'package:uni_links/uni_links.dart';

class ForgotController extends GetxController with BaseController {
  var id = '';
  var email = '';
  StreamSubscription? sub;

  final UserApi _userApi = Get.find<UserApi>();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    initUniLinks();
  }

  Future<Null> initUniLinks() async {
    try {
      String? initialLink = await getInitialLink();

      // Parse the link and warn the user, if it is not correct,
      // but keep in mind it could be `null`.
    } on PlatformException {
      // Handle exception by warning the user their action did not succeed
      // return?
    }
  }

  void forgot() async {
    showLoading(message: 'Please wait...', barrierDismissible: false);
    final Email send_email = Email(
      body: 'Đây nè em trai http://flutterbooksample.com',
      subject: 'Link khôi phụn password',
      recipients: ['samnkse62331@fpt.edu.vn'],
      isHTML: false,
    );

    try {
      hideLoading();
      await FlutterEmailSender.send(send_email);
    } catch (e) {
      print("hàng: ${e}");
    }
    // await _userApi.forgot(email, int.parse(id)).then((value) {
    //   hideLoading();
    //   print(value.error);
    //   if (value.error != true) {
    //     Get.toNamed(Routes.RECOVERY);
    //   }
    // }).catchError((e) {
    //   handleErrorWithToast(e);
    // });
  }
}

方法从链接中打开应用程序。

代码语言:javascript
复制
Future<Null> initUniLinks() async {
    try {
      String? initialLink = await getInitialLink();

      // Parse the link and warn the user, if it is not correct,
      // but keep in mind it could be `null`.
    } on PlatformException {
      // Handle exception by warning the user their action did not succeed
      // return?
    }
  }

main.dart

代码语言:javascript
复制
import 'package:darcy/core/lang/localization_service.dart';
import 'package:darcy/core/storage/spref_helper.dart';
import 'package:darcy/core/theme/app_theme.dart';
import 'package:darcy/injector.dart';
import 'package:darcy/routes/pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  //forced portrait orientation only
  await InitialBinding().setup();
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then((_) {
    runApp(const MyApp());
  });
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Darcy',
      debugShowCheckedModeBanner: false,
      getPages: Pages.pages,
      initialRoute: Routes.SPLASH,
      initialBinding: InitialBinding(),
      theme: ThemeData(
        primarySwatch: AppTheme.colorPrimarySwatch,
      ),
      locale:
          Get.find<SharedPreferencesHelper>().getDefaultLocaleFromLangCode(),
      fallbackLocale: LocalizationService.fallbackLocale,
      translations: LocalizationService(),
    );
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-03-16 11:58:57

我建议你用有名字的路线。

假设您的深层链接类似于https://my.app/new-password,您在这个页面的应用程序中有一个名为new-password的指定路由,然后在您的函数initUnilinks上,得到URI的路径并导航到您想要的路径。

代码语言:javascript
复制
Future<void> initUniLinks() async {
    try {
      Uri? initialLink = await getInitialLink();
      if (initalLink != null) {
        Navigator.of(context).pushNamed(initialLink.path);
      }
      
    } on PlatformException {
      // Handle exception by warning the user their action did not succeed
      // return?
    }
  }

我不熟悉getX,它可能有一些您可以使用的路由。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71495966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档