我是扑朔迷离的新手,正在学习如何通过链接列表和navigator.push按钮使用webview。我想将自定义标题添加到生成的按钮,该怎么做?
我尝试通过直接放置字符串(“ website1”)来编辑Text(url),但是当我这样做时,链接将停止工作。
//这是主页代码:
import 'package:flutter/material.dart';
import 'web_view_container.dart';
class Home extends StatelessWidget {
final _links = [
'http://website1.com',
'http://website2.com/'
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: _links.map((link) => _urlButton(context, link)),
),
),
),
);
}
//buttons
Widget _urlButton(BuildContext context, String url) {
return Container(
padding: EdgeInsets.all(20.0),
child: FlatButton(
color: Theme.of(context).primaryColor,
padding: EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0),
//here the buttons use either the url or a "title", ths problem is ging differente "titles"
child: Text(url),
onPressed: () => _handleURLButtonPress(context, url),
),
);
}
//calls the url
void _handleURLButtonPress(BuildContext context, String url) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => WebViewContainer(url)));
}
}
//这是webview容器的代码
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewContainer extends StatefulWidget {
final url;
WebViewContainer(this.url);
@override
createState() => _WebViewContainerState(this.url);
}
class _WebViewContainerState extends State<WebViewContainer> {
var _url;
final _key = UniqueKey();
_WebViewContainerState(this._url);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(children: [
Expanded(
child: WebView(
key: _key,
javascriptMode: JavascriptMode.unrestricted,
initialUrl: _url))
]));
}
}
预期结果:能够使用带有自定义标题的主页按钮