Flutter实战小项目(4)----UI界面控件的封装 下

接上一个文章,我们还是进行控件的封装。

我们要自己实现一个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<Widget> 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<StatefulWidget> createState() => new WebViewPageState();
}

class WebViewPageState extends State<WebViewPage> {
  final Completer<WebViewController> _controller =
      new Completer<WebViewController>();
  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: <JavascriptChannel>[
              _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<bool> 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)),
        );
      },
    );
  }
}

其余的封装我就不发出来了,最后我会给你们放上地址自己去看!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值