[英]How do I separate a flutter package into another file then call it in main.dart?
I have this simple code in my main.dart which gets the user's current geolocation what I want to do is to create a separate file like get_geolocation.dart and then call it back in my main.dart just to make my main.dart file cleaner, less code in it and more organized.我的 main.dart 中有这个简单的代码,它可以获取用户当前的地理位置我想要做的是创建一个单独的文件,如 get_geolocation.dart 然后在我的 main.dart 中调用它只是为了让我的 main.dart 文件更干净,其中的代码更少,更有条理。 Here's my main.dart code:这是我的 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),
),
);
}
}
Using underscore in method names, class and variables makes it private.在方法名、类和变量中使用下划线使其成为私有的。 so you can only access it where it defined.所以你只能在它定义的地方访问它。 In your code, it's in main.dart.在您的代码中,它位于 main.dart 中。 Here I have removed underscores in get_geolocation.dart.这里我删除了 get_geolocation.dart 中的下划线。
Here I did just like @Omi shah told in comments.在这里,我就像@Omi shah 在评论中所说的那样。
Change as you want.随心所欲地改变。
main.dart主要.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 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),
),
);
}
}
You could create a class to keep your methods and interfaces to geolocation services.您可以创建一个类来保持您的方法和接口与地理定位服务。
file: lib/src/utils/location_controller.dart文件: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();
}
}
Import this class in any widget:在任何小部件中导入此类:
class _HomePageState extends State<HomePage> {
late final locationController = LocationController();
Position? _position;
void _getCurrentLocation() async {
final position = await locationController.myPosition;
setState(() {
_position = position;
});
}
If this "controller" becomes heavy to initialize, you might want to instantiate it in your main and pass down the tree as a parameter or use another method such as Provider.如果这个“控制器”变得难以初始化,您可能希望在 main 中实例化它并将树作为参数向下传递或使用其他方法,例如 Provider。
You can create your apps in flutter using the skeleton template:您可以使用骨架模板在 Flutter 中创建应用程序:
flutter create -t skeleton my_app
It will give some ideia on how to structure your files and lots of best practices.它将提供一些关于如何构建文件和许多最佳实践的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.