Flutter地点选择插件google_places_dialog的使用

Flutter地点选择插件google_places_dialog的使用

安装 💻

在开始使用google_places_dialog之前,你需要确保已经在你的机器上安装了Flutter SDK

pubspec.yaml文件中添加google_places_dialog依赖:

dependencies:
  google_places_dialog:

然后运行以下命令来安装它:

flutter packages get

持续集成 🤖

google_places_dialog自带了一个由GitHub Actions驱动的内置持续集成工作流,该工作流由Very Good Workflows提供支持。你也可以添加自己的CI/CD解决方案。

默认情况下,在每次拉取请求或推送时,CI会格式化、静态检查和测试代码。这确保了代码的一致性和正确性,即使你在添加功能或进行更改时也是如此。项目使用Very Good Analysis来执行我们的团队使用的严格分析选项。代码覆盖率使用Very Good Workflows进行强制执行。

运行测试 🧪

对于初次使用的用户,安装very_good_cli

dart pub global activate very_good_cli

要运行所有单元测试:

very_good test --coverage

要查看生成的覆盖率报告,可以使用lcov

# 生成覆盖率报告
genhtml coverage/lcov.info -o coverage/

# 打开覆盖率报告
open coverage/index.html

示例代码

以下是一个完整的示例,展示了如何使用google_places_dialog插件来选择地点:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Google Places Dialog Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              final result = await GooglePlacesDialog.show(context);
              if (result != null) {
                // 处理选择的地点
                print('Selected Location: ${result.name}');
              }
            },
            child: Text('Select Location'),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter地点选择插件google_places_dialog的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter地点选择插件google_places_dialog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用google_places_dialog插件来选择地点的示例代码。这个插件允许你使用Google Places API来显示一个地点选择对话框,并获取用户选择的地点信息。

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

dependencies:
  flutter:
    sdk: flutter
  google_places_dialog: ^latest_version  # 请替换为最新版本号

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

接下来,你需要配置Google Places API的API密钥。这通常是通过Android和iOS的原生配置文件来完成的。

对于Android

  1. android/app/src/main/AndroidManifest.xml中添加以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  1. android/app/src/main/res/values/strings.xml中添加你的Google API密钥:
<resources>
    <string name="google_api_key">YOUR_GOOGLE_API_KEY</string>
</resources>

对于iOS

  1. ios/Runner/Info.plist中添加以下权限:
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要访问您的位置以选择地点</string>
  1. ios/Runner/AppDelegate.swift中,你可以通过Info.plist读取API密钥(或者硬编码,但出于安全考虑,建议通过配置文件管理):
import UIKit

@UIApplicationMain
@objc class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 这里可以读取或设置API密钥,但通常通过Info.plist或其他安全方式管理
        return true
    }
}

注意:iOS中通常不会直接在代码中暴露API密钥,而是通过环境变量或其他安全方式管理。

Flutter代码示例

接下来,在你的Flutter代码中,你可以这样使用google_places_dialog插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Google Places Dialog Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _selectedPlace = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Google Places Dialog Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Place:',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 10),
            Text(
              _selectedPlace,
              style: TextStyle(fontSize: 20, color: Colors.blue),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => _showPlacePicker(),
              child: Text('Select Place'),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _showPlacePicker() async {
    try {
      PlaceResult result = await GooglePlacesDialog.show(
        apiKey: 'YOUR_GOOGLE_API_KEY', // 替换为你的API密钥
        context: context,
        mode: PlaceMode.full,
        enableReturnKeyAlways: true,
        biasLocation: null, // 可选:设置搜索偏好的地理位置
        radius: 1500.0, // 可选:设置搜索半径,单位为米
        language: 'en', // 可选:设置搜索语言
      );

      if (result != null) {
        setState(() {
          _selectedPlace = result.place.name + ", " + result.place.vicinity;
        });
      }
    } catch (e) {
      print('Error: $e');
    }
  }
}

请注意,直接在代码中硬编码API密钥是不安全的。在实际应用中,你应该使用环境变量或安全存储来管理这些敏感信息。

这样,你就可以在Flutter应用中集成并使用google_places_dialog插件来选择地点了。

回到顶部