问题描述
问题是,当我想从回调(由插件调用)进行导航时,新页面将作为小部件推入页面中.
The problem is when I want to navigate from a callback - which is invoked by plugin - new page is pushed in as a widget inside my page.
这是代码:
import 'dart:async';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
) );
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String barcode = "";
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Barcode Scanner Example'),
),
body: new Center(
child: new Column(
children: <Widget>[
new Container(
child: new MaterialButton(
onPressed: scan, child: new Text("Scan")),
padding: const EdgeInsets.all(8.0),
),
],
),
));
}
Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
print("${context}");
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return new BarcodePage(barcode);}
));
setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException{
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}
}
class BarcodePage extends StatefulWidget {
BarcodePage(String s) {
str = s;
}
String str;
@override
State<StatefulWidget> createState() {
return _BarcodePageState(str);
}
}
class _BarcodePageState extends State<BarcodePage> {
String str;
_BarcodePageState(String s ){
str = s;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Bar code"),),
body: new Text(str),
);
}
}
您可以在 https://github.com/arashbi/flutter_barcode_reader 示例文件夹中找到该应用程序
You can find the application in https://github.com/arashbi/flutter_barcode_reader example folder
这与我之前的问题有关,但设置较简单.
This is related to my question before, but simpler setup.
推荐答案
AFAI理解问题是在渲染管线的中间发生了回调,这会导致错误的行为.解决方案是使用Future.delayed
或SchedulerBinding.instance.addPostFrameCallback
AFAI understand the problem is that callback is happening in the middle of render pipeline, and it causes the wrong behaviour. The solution is to use either Future.delayed
or SchedulerBinding.instance.addPostFrameCallback
这些方法使导航发生在渲染管道之后,并且导航器可以正确执行其工作
These methods causes the navigation to happen after the render pipeline, and the navigator can do its job properly
这篇关于从插件回调导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!