接上一个文章,我们还是进行控件的封装。
我们要自己实现一个AppBar控件,要知道我们AppBar控件有自己的高度,那么要实现自己的封装还要进行一个接口的实现 PreferredSizeWidget,当然你也可以用相关的控件把他包起来。
话不多说上代码:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app_pneumonia/config/const.dart';
class ComMomBar extends StatelessWidget implements PreferredSizeWidget {
const ComMomBar({
this.title = '',
this.showShadow = false,
this.rightDMActions,
this.backgroundColor = appBarColor,
this.mainColor = Colors.white,
this.titleW,
this.bottom,
this.leadingImg = '',
this.leadingW,
});
final String title;
final bool showShadow;
final List rightDMActions;
final Color backgroundColor;
final Color mainColor;
final Widget titleW;
final Widget leadingW;
final PreferredSizeWidget bottom;
final String leadingImg;
@override
Size get preferredSize => new Size(100, 50);
Widget leading(BuildContext context) {
final bool isShow = Navigator.canPop(context);
if (isShow) {
return new InkWell(
child: new Container(
width: 15,
height: 28,
child: leadingImg != ''
? new Image.asset(leadingImg)
: new Icon(CupertinoIcons.back, color: mainColor),
),
onTap: () {
if (Navigator.canPop(context)) {
FocusScope.of(context).requestFocus(new FocusNode());
Navigator.pop(context);
}
},
);
} else {
return null;
}
}
@override
Widget build(BuildContext context) {
return showShadow
? new Container(
decoration: BoxDecoration(
border: Border(
bottom: new BorderSide(
color: Colors.grey, width: showShadow ? 0.5 : 0.0))),
child: new AppBar(
title: titleW == null
? new Text(
title,
style: new TextStyle(
color: mainColor,
fontSize: 17.0,
fontWeight: FontWeight.w500),
)
: titleW,
backgroundColor: mainColor,
elevation: 0.0,
brightness: Brightness.dark,
leading: leadingW ?? leading(context),
centerTitle: true,
actions: rightDMActions ?? [new Center()],
bottom: bottom != null ? bottom : null,
),
)
: new AppBar(
title: titleW == null
? new Text(
title,
style: new TextStyle(
color: mainColor,
fontSize: 17.0,
fontWeight: FontWeight.w500),
)
: titleW,
backgroundColor: backgroundColor,
elevation: 0.0,
brightness: Brightness.dark,
leading: leadingW ?? leading(context),
centerTitle: false,
bottom: bottom != null ? bottom : null,
actions: rightDMActions ?? [new Center()],
);
}
}
还有我们点击首页的内容,我们直接嵌入了一个网页,直接看代码就行了,说实话这东西没什么好讲的。:
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_pneumonia/widget/bar/commom_bar.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewPage extends StatefulWidget {
final String url;
final String title;
WebViewPage(this.url, this.title);
@override
State createState() => new WebViewPageState();
}
class WebViewPageState extends State {
final Completer _controller =
new Completer();
WebViewController _webViewController;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: new ComMomBar(title: widget.title ?? ''),
body: new WillPopScope(
child: new Builder(builder: (BuildContext context) {
return new WebView(
initialUrl: widget.url,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
_webViewController = webViewController;
},
javascriptChannels: [
_toasterJavascriptChannel(context),
].toSet(),
navigationDelegate: (NavigationRequest request) {
if (request.url
.startsWith('https://github.com/ahyangnb/ncov_2019')) {
print('blocking navigation to $request}');
return NavigationDecision.prevent;
}
print('allowing navigation to $request');
return NavigationDecision.navigate;
},
onPageFinished: (String url) {
print('Page finished loading: $url');
},
);
}),
onWillPop: () => pop(),
),
);
}
Future pop() async {
bool canGoBack = await _webViewController.canGoBack();
if (canGoBack) {
_webViewController.goBack();
return false;
} else {
return true;
}
}
JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
return new JavascriptChannel(
name: 'Toaster',
onMessageReceived: (JavascriptMessage message) {
Scaffold.of(context).showSnackBar(
new SnackBar(content: Text(message.message)),
);
},
);
}
}
其余的封装我就不发出来了,最后我会给你们放上地址自己去看!