我的 main.dart 中有這個簡單的代碼,它可以獲取用戶當前的地理位置我想要做的是創建一個單獨的檔案,例如 get_geolocation.dart 然后在我的 main.dart 中呼叫它只是為了讓我的 main.dart 檔案更干凈,其中的代碼更少,更有條理。這是我的 main.dart 代碼:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Position? _position;
void _getCurrentLocation() async {
Position position = await _determinePosition();
setState(() {
_position = position;
});
}
Future<Position> _determinePosition() async {
LocationPermission permission;
permission = await Geolocator.checkPermission();
if(permission== LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if(permission == LocationPermission.denied){
return Future.error('Location Permissions are denied');
}
}
return await Geolocator.getCurrentPosition();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('geolocator'),
centerTitle: true,
),
body: Center(
child: _position != null
? Text('Current position: ' _position.toString())
: Text('No Location Data'),
),
floatingActionButton: FloatingActionButton(
onPressed: _getCurrentLocation,
tooltip: 'increment',
child: const Icon(Icons.add),
),
);
}
}
uj5u.com熱心網友回復:
在方法名、類和變數中使用下劃線使其成為私有的。所以你只能在它定義的地方訪問它。在您的代碼中,它位于 main.dart 中。這里我洗掉了 get_geolocation.dart 中的下劃線。
在這里,我就像@Omi shah 在評論中所說的那樣。
- 提取小部件并移入新檔案。
- 匯入包名。
隨心所欲地改變。
主要.dart
import 'package:flutter/material.dart';
// import 'package:geolocator/geolocator.dart';
import 'get_geolocation.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
get_geolocation.dart
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Position? _position;
void getCurrentLocation() async {
Position position = await determinePosition();
setState(() {
_position = position;
});
}
Future<Position> determinePosition() async {
LocationPermission permission;
permission = await Geolocator.checkPermission();
if(permission== LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if(permission == LocationPermission.denied){
return Future.error('Location Permissions are denied');
}
}
return await Geolocator.getCurrentPosition();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('geolocator'),
centerTitle: true,
),
body: Center(
child: _position != null
? Text('Current position: ' _position.toString())
: Text('No Location Data'),
),
floatingActionButton: FloatingActionButton(
onPressed: _getCurrentLocation,
tooltip: 'increment',
child: const Icon(Icons.add),
),
);
}
}
uj5u.com熱心網友回復:
您可以創建一個類來保持您的方法和介面與地理定位服務。
檔案:lib/src/utils/location_controller.dart
class LocationController {
LocationController();
// This way you create only one instance of Geolocator
// instead of creating a new instance every time the method is called
late final geolocator = Geolocator();
Future<Position> get myPosition async {
// Place additional logic
return await geolocator.getCurrentPosition();
}
}
在任何小部件中匯入此類:
class _HomePageState extends State<HomePage> {
late final locationController = LocationController();
Position? _position;
void _getCurrentLocation() async {
final position = await locationController.myPosition;
setState(() {
_position = position;
});
}
如果這個“控制器”變得難以初始化,您可能希望在主程式中實體化它并將樹作為引數向下傳遞或使用其他方法,例如 Provider。
您可以使用骨架模板在 Flutter 中創建應用程式:
flutter create -t skeleton my_app
它將提供一些關于如何構建檔案和許多最佳實踐的想法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/494753.html
上一篇:合并地圖的值并將其插入另一個地圖
