【发布时间】:2020-12-26 12:32:21
【问题描述】:
目标
我使用bottomNavigationBar 在几页之间切换。其中两个页面使用WebView 显示本地html 文件。
问题
我发现当我在这些页面之间切换时,带有纯 Flutter 小部件的页面可以完美加载。但是 HTML 页面只会在切换时显示最初加载的页面。
例如,如果我有两个导航按钮通向 Page1 和 Page2,那么在运行时,如果我先点击 Page1 按钮,然后点击 Page2 按钮,那么 WebView 仍将显示 Page1 而不是 Page2。这是错误的。
代码
这是我使用的 HTML 页面代码
class LocalLoader {
Future<String> loadLocal(String filename) async {
return await rootBundle.loadString('assets/doc/$filename');
}
}
class HtmlPage extends StatelessWidget {
final String htmlFile;
HtmlPage({
@required this.htmlFile
});
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder<String>(
future: LocalLoader().loadLocal(htmlFile),
builder: (context, snapshot) {
if (snapshot.hasData) {
return WebView(
initialUrl: Uri.dataFromString(snapshot.data,
mimeType: 'text/html',
// CAUTION
// - required for non-ascii chars
encoding: Encoding.getByName("UTF-8")
).toString(),
javascriptMode: JavascriptMode.unrestricted,
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
print('undefined behaviour');
}
return CircularProgressIndicator();
},
),);
}
}
然后用我的bottomNavigationBar,处理点击事件:
class MyFlutterView extends StatefulWidget {
@override
_MyFlutterViewState createState() => _MyFlutterViewState();
}
class _MyFlutterViewState extends State<MyFlutterView> {
final Keys keys = Keys();
int _iSelectedDrawerItem = 3; // self
int _iSelectedNavItem = 0;
static List<Widget> _widgetOptions = <Widget>[
MyFlutterPlaceholder(title: 'Index 0: MyFlutter'),
MyPage(htmlFile: 'page1.html'),
MyPage(htmlFile: 'page2.html'),
];
void _onItemTapped(int index) {
setState(() {
_iSelectedNavItem = index;
});
}
@override
Widget build(BuildContext context) {
final deviceSize = MediaQuery.of(context).size;
final appBar = AppBar(
backgroundColor: WidgetColors.menubar,
title: Text('MyFlutter'),
);
return Scaffold(
appBar: appBar,
endDrawer: NavDrawer(
keys: keys,
iSelectedDrawerItem: _iSelectedDrawerItem,
),
body: Container(
decoration: BoxDecoration(
gradient: WidgetColors.canvas,
),
child: _widgetOptions.elementAt(_iSelectedNavItem),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex : _iSelectedNavItem,
type: BottomNavigationBarType.fixed,
backgroundColor: WidgetColors.menubar,
fixedColor: WidgetColors.myColor,
// selectedItemColor: WidgetColors.myColor,
unselectedItemColor: Colors.white,
selectedIconTheme: IconThemeData(color: WidgetColors.myColor),
// unselectedIconTheme: IconThemeData(color: Colors.white),
items: [
BottomNavigationBarItem(
label: 'MyFlutter',
icon: Icon(Icons.build)
),
BottomNavigationBarItem(
label: 'Page1-HTML',
icon: Icon(Icons.help,),
),
BottomNavigationBarItem(
label: 'Page2-HTML',
icon: Icon(Icons.info_outline_rounded),
),
],
onTap: _onItemTapped),
);
}
}
我也试过StatefulWidgets,但问题仍然存在。
解决方法
我现在唯一的解决方法是从我拥有的每个页面的 HtmlPage 类派生,如下所示:
class Page1 extends HtmlPage {
Page1() : super(htmlFile: 'page1.html');
}
class Page2 extends HtmlPage {
Page2() : super(htmlFile: 'page2.html');
}
此后,HTML 页面将按预期切换和加载。
问题
我应该如何解决这个问题?我应该更明确地加载 HTML 文件吗?我认为setState 会自动为我处理加载,这当然适用于纯颤动小部件页面(上面代码中的MyFlutterPlaceholder 类)。
另外,我确保每次通过导航栏切换页面时都会调用 url 加载。
【问题讨论】:
-
它可以构建吗? “底部导航栏”中没有“标题”参数。当我通过删除未提供的代码来测试您的代码时,它运行良好。
-
@KuKu 是的,它是可构建的。我检查了我的帖子并修复了降价语法错误。但似乎
title是 AppBar 属性。