Flutter自动填充视图插件pataa_autofill_view的使用

Flutter自动填充视图插件pataa_autofill_view的使用

官方地址自动填充SDK适用于Flutter以访问Pataa API。该插件支持iOS和Android。

Android iOS
支持 SDK 21+ 10.0+

Pataa AutoFill SDK实现指南

减少流失率(即客户在您的网站上将商品加入购物车后但在下单前放弃)的主要原因之一是地址填写过程冗长且繁琐。Pataa Navigations为此问题提供了一个完美的解决方案,即Pataa地址填充API。我们的地址应用Pataa将一个长而复杂的地址转换为独特的短码,如JAIN53, KUMAR100,并通过分享精确的地理位置标签、用户推荐路线、用户录制的语音方向等简化复杂的地址系统,从而使得最后一公里导航变得简单。现在,客户只需在地址栏输入Pataa码(^KUMAR100),即可轻松导航到目的地,而无需任何麻烦。

特性

使用此插件可以在您的Flutter应用中:

  • 将一个长而复杂的地址转换为唯一的短码,如JAIN53, KUMAR100。
  • 通过分享精确的地理位置标签来简化复杂的地址系统。
  • 提供用户推荐路线、用户录制的语音方向等,从而使得最后一公里导航变得容易。

开始使用

该插件依赖于原生iOS和Android SDK。这些原生SDK提供了获取Pataa地址详情的服务。

使用方法

要使用该插件,只需在pubspec.yaml文件中添加pataa_autofill_view: ^1.0.5并运行pub get

此外,您还需要iOS密钥、iOS前缀和Android密钥才能使用该插件。

安装SDK

要使用Pataa自动填充Flutter SDK:

在您的包的pubspec.yaml中添加以下内容(并运行隐式dart pub get):

pataa_autofill_view: ^1.0.5

转到iOS文件夹并在终端中运行pod install

如果您遇到以下错误:

[!] CocoaPods could not find compatible versions for pod "pataa_autofill_view":
In Podfile:
pataa_autofill_view (from `.symlinks/plugins/pataa_autofill_view/ios`)

Specs satisfying the `pataa_autofill_view (from .symlinks/plugins/pataa_autofill_view/ios)` dependency were found, but they required a higher minimum deployment target.

只需将Xcode项目的最小部署目标更新为10.0。

示例

// TODO: 插入您的密钥。

PataaAutoFillView pataaView = PataaAutoFillView(
    secretKeyiOS: "===插入您的iOS密钥===",
    appPrefixiOS: "===插入您的iOS前缀===",
    secretKeyAndroid: '===插入您的Android密钥===',
    widthAddressInputField: MediaQuery.of(context).size.width - 10,
    heightAddressInputField:
        defaultTargetPlatform == TargetPlatform.iOS ? 100 : 120,
    onLoadData: (data) {
        if (kDebugMode) {
            print('Pataa Details :- $data');
        }
    },
);

更多示例

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:pataa_autofill_view/pataa_autofill_view.dart';

void main() {
  HttpOverrides.global = MyHttpOverrides();

  runApp(const MyApp());
}

class MyHttpOverrides extends HttpOverrides {
  [@override](/user/override)
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  [@override](/user/override)
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String pataaCode = "";
  String address1 = "";
  String address2 = "";
  String address3 = "";
  String address4 = "";
  String zipcode = "";
  String cityName = "";
  String stateCode = "";
  String stateName = "";
  String countryCode = "";
  String countryName = "";
  String mapLink = "";
  String firstName = "";
  String lastName = "";
  String userCountryCode = "";
  String mobile = "";
  String error = "";

  bool isResponse = false;

  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    // TODO: 插入您的密钥。
    PataaAutoFillView pataaView = PataaAutoFillView(
      secretKeyiOS: "===插入您的iOS密钥===",
      appPrefixiOS: "===插入您的iOS前缀===",
      secretKeyAndroid: '===插入您的Android密钥===',
      widthAddressInputField: MediaQuery.of(context).size.width - 10,
      heightAddressInputField:
          defaultTargetPlatform == TargetPlatform.iOS ? 100 : 120,
      onLoadData: (data) {
        // if (TargetPlatform.iOS == defaultTargetPlatform) {
        //   FocusManager.instance.primaryFocus?.unfocus();
        // }

        isResponse = true;
        setState(() {
          firstName = "";
          lastName = "";
          address4 = "";
          address3 = "";
          zipcode = "";
          stateName = "";
          address1 = "";
          userCountryCode = "";
          stateCode = "";
          countryName = "";
          pataaCode = "";
          cityName = "";
          countryCode = "";
          mapLink = "";
          address2 = "";
          mobile = "";
          error = "";
          if (data.containsKey("error")) {
            error = data["error"];
          } else {
            firstName = data["firstName"] ?? "";
            lastName = data["lastName"] ?? "";
            address4 = data["address4"] ?? "";
            address3 = data["address3"] ?? "";
            zipcode = data["zipcode"] ?? "";
            stateName = data["stateName"] ?? "";
            address1 = data["address1"] ?? "";
            countryName = data["countryName"] ?? "";

            userCountryCode = data["userCountryCode"] ?? "";
            stateCode = data["stateCode"] ?? "";
            pataaCode = data["pataaCode"] ?? "";
            cityName = data["cityName"] ?? "";
            if (countryName != "Unnamed location") {
              countryCode = data["countryCode"] ?? "";
            }

            mapLink = data["mapLink"] ?? "";
            address2 = data["address2"] ?? "";
            mobile = data["mobile"] ?? "";
          }
        });
      },
    );

    TextStyle getTextStyle({Color color = Colors.black}) =>
        TextStyle(color: color, fontSize: 16);

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Pataa Auto Fill'),
        ),
        body: SingleChildScrollView(
          keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  pataaView,
                  isResponse && error.isEmpty
                      ? Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "First Name: $firstName",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Last Name: $lastName",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Address1: $address1",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Address2: $address2",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Address3: $address3",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Address4: $address4",
                              style: getTextStyle(),
                            ),
                            Text(
                              "City Name: $cityName",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Country Name: $countryName",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Country Code: $countryCode",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Mobile: $mobile",
                              style: getTextStyle(),
                            ),
                            Text(
                              "State: $stateName",
                              style: getTextStyle(),
                            ),
                            Text(
                              "State Code: $stateCode",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Pataa Code: $pataaCode",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Map Link: $mapLink",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Zip Code: $zipcode",
                              style: getTextStyle(),
                            ),
                            Text(
                              "Mobile Country Code: $userCountryCode",
                              style: getTextStyle(),
                            ),
                          ],
                        )
                      : isResponse
                          ? Text(
                              "Error: $error",
                              style: getTextStyle(color: Colors.red),
                            )
                          : Container(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter自动填充视图插件pataa_autofill_view的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自动填充视图插件pataa_autofill_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter中使用pataa_autofill_view插件的代码示例。pataa_autofill_view是一个用于在Flutter应用中实现自动填充功能的插件,它可以帮助开发者简化表单的自动填充流程,特别是在处理密码管理器或类似功能时非常有用。

首先,你需要在你的pubspec.yaml文件中添加这个插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  pataa_autofill_view: ^最新版本号  # 请替换为实际的最新版本号

然后,运行flutter pub get来安装依赖。

接下来是一个简单的使用示例,展示如何在Flutter中使用pataa_autofill_view

import 'package:flutter/material.dart';
import 'package:pataa_autofill_view/pataa_autofill_view.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: AutofillExampleScreen(),
    );
  }
}

class AutofillExampleScreen extends StatefulWidget {
  @override
  _AutofillExampleScreenState createState() => _AutofillExampleScreenState();
}

class _AutofillExampleScreenState extends State<AutofillExampleScreen> {
  final _formKey = GlobalKey<FormState>();
  String? username;
  String? password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Autofill Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              AutofillTextField(
                labelText: 'Username',
                onSaved: (value) {
                  username = value;
                },
                autofillHints: [AutofillHints.username],
              ),
              SizedBox(height: 16),
              AutofillTextField(
                labelText: 'Password',
                obscureText: true,
                onSaved: (value) {
                  password = value;
                },
                autofillHints: [AutofillHints.password],
              ),
              SizedBox(height: 24),
              ElevatedButton(
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                    _formKey.currentState!.save();
                    // 在这里处理表单数据,例如发送到服务器
                    print('Username: $username');
                    print('Password: $password');
                  }
                },
                child: Text('Submit'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class AutofillTextField extends StatelessWidget {
  final String labelText;
  final bool? obscureText;
  final List<AutofillHints> autofillHints;
  final ValueChanged<String?> onSaved;

  const AutofillTextField({
    required this.labelText,
    this.obscureText,
    required this.autofillHints,
    required this.onSaved,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      decoration: InputDecoration(labelText: labelText),
      obscureText: obscureText,
      autofillHints: autofillHints,
      onSaved: onSaved,
      validator: (value) {
        if (value == null || value.isEmpty) {
          return 'This field is required.';
        }
        return null;
      },
    );
  }
}

在这个示例中,我们创建了一个简单的表单,包含用户名和密码字段。这两个字段都使用了AutofillTextField(一个自定义的封装了TextFormField的Widget,用于简化代码),并且分别设置了AutofillHints.usernameAutofillHints.password作为它们的自动填充提示。

当用户点击提交按钮时,如果表单验证通过,则表单数据会被保存,并打印到控制台。

请注意,为了完全测试自动填充功能,你可能需要在支持自动填充功能的设备或模拟器上运行此代码,并配置一个密码管理器或类似的工具来提供自动填充数据。

希望这个示例能帮助你理解如何在Flutter中使用pataa_autofill_view插件。如果你有任何其他问题,欢迎继续提问!

回到顶部