当用户返回父窗口小部件然后返回到Webview时,我一直试图保持页面滚动。
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text(
'Webview Test',
),
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (context) => WebviewTest())),
),
],
),
),
);
}
}
class WebviewTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Webview Test',
),
),
body: WebView(
initialUrl: 'https://wikipedia.org/wiki/Flutter_(software)',
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}
在此示例代码中,每次按下“ Webview测试”按钮,都会重新构建Webview小部件,并且页面滚动和展开的选项卡都将丢失。 有什么方法可以防止这种情况的发生并将页面保留在用户离开的地方?