自动浏览颤振屏幕



我正在开发一个使用位置数据的应用程序。因此,在着陆屏幕中,我将检查是否授予访问位置数据的权限。我使用地理定位器软件包来完成此操作。当用户授予权限时,我会收集当前位置等数据,然后我想自动移动到另一个屏幕,即应用程序的主屏幕。有什么办法让我做到这一点吗?

我正在制作一个简单的应用程序来演示基于结果的权限检查和导航的正确处理。您可以查看以下代码和注释来了解流程:

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SomeScreen(),
);
}
}
class SomeScreen extends StatelessWidget {
final _scaffoldKey = GlobalKey<ScaffoldState>();
void _determinePosition(context) async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();

// Checking if the service is enabled
if (!serviceEnabled) {
// If not, display a Snackbar
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text('Location services are disabled.'),
duration: Duration(seconds: 2),
));
}
// Checking the permission here
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(
'Location permissions are permanently denied, we cannot request permissions.'),
duration: Duration(seconds: 2),
));
}
if (permission == LocationPermission.denied) {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text('Location permissions are denied'),
duration: Duration(seconds: 2),
));
}
}
// Finally if the permission is granted, navigate the user to new screen
Position position = await Geolocator.getCurrentPosition();
Navigator.push(context,
MaterialPageRoute(builder: (context) => Home(position: position)));
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: Container(
alignment: Alignment.center,
color: Colors.blue,
child: FlatButton(
onPressed: () => _determinePosition(context),
child: Text("Check permission"),
color: Colors.red,
),
),
);
}
}
//  A simple screen to display the position
class Home extends StatefulWidget {
final Position position;
Home({this.position});
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: Text(
'Location: ${widget.position.altitude} ${widget.position.longitude}'),
),
),
);
}
}

如果你想让你的主屏幕依赖于一个值或条件,我认为你可以在家里使用一个Streambuilder,它的streamcontroller接收一个布尔值,这取决于它将返回一个或另一个屏幕。

例如,我在这里使用了这个Streambuilder,它监听布尔值。我将通过按下按钮将这个布尔值更改为true。在我家的Streambuilder中,我有一个条件,即如果收到的信息为真,则snapshot.data不应返回MyHomePage,而应返回OtherRoute。

import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
final StreamController<bool> _streamController =
StreamController<bool>.broadcast();
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: StreamBuilder(
stream: _streamController.stream,
builder: (context, snapshot) {
if(snapshot.data == true){
return TheOtherRoute();
}
return MyHomePage();
},
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('this is a testo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: FlatButton(
color: Colors.black38,
child: Text('Change boolean'),
onPressed: () {
_streamController.add(true);
},
),
),
],
),
),
);
}
}
class FirstRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Route'),
),
body: Center(
child: Text('Your boolean changed. Your home page is now FirstRoute()'),
),
);
}
}

试试看,让我知道。

相关内容

  • 没有找到相关文章

最新更新