Я использовал несколько веб-просмотров с отдельными BottomNavigationBarItem
вкладками и загружал разные URL
в каждой вкладке.
Проблема в том, что при первом открытии первый URL-адрес TAB загружается идеально, но когда я нажимаю наВторая вкладка, в которой отображается первая вкладка Webview. Третья вкладка содержит обычный текст Widget
, если вы вернулись с третьей вкладки, то работаете отлично. Но нажмите на первую и вторую вкладку. Всегда отображать последнюю загруженную URL
мой код здесь
Домашний класс
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'web_view_container.dart';
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HomeState();
}
}
class _HomeState extends State<Home> {
int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
List<Widget> _widgetOptions = <Widget>[
WebViewContainer('https://offerscouponsdeals.in/'),
WebViewContainer('https://flutter.io'),
Text(
'Index 2: School',
style: optionStyle,
),
];
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('RSVP App'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('School'),
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
}
Класс контейнера WebView
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class _WebViewContainerState extends State < WebViewContainer >{
var _url;
final _key = UniqueKey();
_WebViewContainerState(this._url);
num _stackToView = 1;
void _handleLoad(String value) {
setState(() {
_stackToView = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _stackToView,
children: [
Column(
children: < Widget > [
Expanded(
child: WebView(
key: _key,
javascriptMode: JavascriptMode.unrestricted,
initialUrl: _url,
onPageFinished: _handleLoad,
)
),
],
),
Container(
color: Colors.white,
child: Center(
child: CircularProgressIndicator(),
),
),
],
)
);
}
}
class WebViewContainer extends StatefulWidget {
final url;
WebViewContainer(this.url);
@override
createState() => _WebViewContainerState(this.url);
}
Может кто-нибудь сказать мне, где я не прав? Заранее спасибо