如何将一个flutter包分离到另一个文件中,然后在main.dart中调用它?



我有这个简单的代码在我的主。Dart获取用户当前的地理位置我要做的是创建一个单独的文件,比如get_geolocation。先打一枪,然后再打回来。这是我的主菜单。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),
),
);
}
}

在方法名、类和变量中使用下划线使其私有。所以你只能在它定义的地方访问它。在代码中,它在main。dart中。这里我删除了get_geolocation.dart.

中的下划线我就像@Omi shah在评论中说的那样做了。

  1. 提取小部件并移动到新文件
  2. 导入包名。

随你怎么改

main.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),
),
);
}
}

你可以创建一个类来保存你的方法和接口的地理定位服务。

文件:lib/src/跑龙套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;
});
}

如果this "controller"变得难以初始化,你可能想要在main中实例化它,并将树作为参数向下传递,或者使用其他方法,如Provider。

您可以使用骨架模板在flutter中创建应用程序:

flutter create -t skeleton my_app 

它会给你一些关于如何组织你的文件和许多最佳实践的想法。

相关内容

最新更新