Flutter城市选择器插件city_picker_from_map的使用
Flutter城市选择器插件city_picker_from_map的使用
城市选择器插件City Picker From Map
city_picker_from_map
是一个用于从SVG地图中选择城市的Flutter插件。
支持的国家(超过150个)可以在 这里 查看。
截图
开始使用
在你的Flutter项目的 pubspec.yaml
文件中,添加以下依赖:
dependencies:
...
city_picker_from_map: any
在你的库文件中添加以下导入语句:
import 'package:city_picker_from_map/city_picker_from_map.dart';
使用方法
CityPickerMap(
width: double.infinity,
height: double.infinity,
map: Maps.TURKEY,
onChanged: (city) {
setState(() {
selectedCity = city;
});
},
actAsToggle: true,
dotColor: Colors.white,
selectedColor: Colors.lightBlueAccent,
strokeColor: Colors.white24,
)
属性
属性名 | 类型 | 描述 |
---|---|---|
key | Key? | 键值 |
map | String | 想要展示的地图名称。使用 Maps.<MAP_NAME> 形式。所有地图都在 maps.dart 中 |
width | double? | 地图宽度,默认值为 double.infinity |
height | double? | 地图高度,默认值为 double.infinity |
dotColor | Color? | 城市中心点的颜色 |
strokeColor | Color? | 城市边界的颜色 |
selectedColor | Color? | 选中城市的颜色 |
actAsToggle | boolean? | 城市选择行为是否像切换按钮一样 |
onChanged | Function(City? city) | 当城市改变时返回新的城市值 |
完整示例
以下是一个完整的示例代码,展示了如何使用 city_picker_from_map
插件:
import 'package:flutter/material.dart';
import 'package:city_picker_from_map/city_picker_from_map.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomeView(),
theme: ThemeData.dark(),
);
}
}
class HomeView extends StatefulWidget {
[@override](/user/override)
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
City? selectedCity;
final GlobalKey<CityPickerMapState> _mapKey = GlobalKey();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Selected City: ${selectedCity?.title ?? '(?)'}'),
actions: [
IconButton(
icon: Icon(Icons.delete, color: Colors.red),
onPressed: () {
_mapKey.currentState?.clearSelect();
setState(() {
selectedCity = null;
});
},
)
],
),
body: Center(
child: InteractiveViewer(
scaleEnabled: true,
panEnabled: true,
constrained: true,
child: CityPickerMap(
key: _mapKey,
width: double.infinity,
height: double.infinity,
map: Maps.TURKEY,
onChanged: (city) {
setState(() {
selectedCity = city;
});
},
actAsToggle: true,
dotColor: Colors.white,
selectedColor: Colors.lightBlueAccent,
strokeColor: Colors.white24,
),
),
),
);
}
}
更多关于Flutter城市选择器插件city_picker_from_map的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter城市选择器插件city_picker_from_map的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用city_picker_from_map
插件的一个代码示例。这个插件允许用户从地图上选择一个城市。
首先,确保你已经将city_picker_from_map
添加到你的pubspec.yaml
文件中:
dependencies:
flutter:
sdk: flutter
city_picker_from_map: ^latest_version # 请将latest_version替换为当前最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤使用CityPickerFromMap
:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:city_picker_from_map/city_picker_from_map.dart';
- 创建一个按钮来触发城市选择器:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('City Picker Example'),
),
body: Center(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? selectedCityName;
void _pickCity() async {
final result = await CityPickerFromMap.showCityPicker(
context,
apiKey: 'YOUR_GOOGLE_MAPS_API_KEY', // 请替换为你的Google Maps API Key
defaultLocation: 'China', // 设置默认显示的国家或地区
language: 'zh', // 设置语言,例如中文(zh)或英文(en)
);
if (result != null) {
setState(() {
selectedCityName = result.name;
});
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
selectedCityName ?? '请选择一个城市',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _pickCity,
child: Text('选择城市'),
),
],
);
}
}
在上面的代码中,_pickCity
方法会弹出一个城市选择器对话框,用户可以从地图上选择一个城市。选择完成后,result
将包含城市的名称和其他相关信息,你可以根据需要处理这些信息。
请注意:
- 你需要提供一个有效的Google Maps API Key。
- 确保你已经在Google Cloud Platform中启用了所需的API,并设置了正确的账单信息(尽管对于某些用途,Google Maps API有免费配额)。
这个示例展示了如何使用city_picker_from_map
插件的基本功能。你可以根据需求进一步自定义和扩展它。