开发中,必须要用到的也就这三个小插件了。这里主要讲一下 EventBus,轮播图和沉浸式状态栏教程很多并且很简单。
下面主要用上面三个实现的功能,b站广告图,沉浸式状态栏,以及点击左上角头像用 eventbus 进入我的页面,效果图如下:
下载链接,建议去 CSDN 下载,不要积分,CSDN 是阶段性代码,GitHub 有时忘记传上去:
CSDN :flutter_blbl.zip-Android文档类资源-CSDN下载
GitHub:https://github.com/wuqingsen/FlutterLearnDemo
EventBus
EventBus 在 Android 就是特别常用的,用来隔着页面发送消息,处理一些事情;在 Flutter 中我自然也是想到了它。
在这里我对它的封装方式很类似与我在 Android 中封装方式:封装到 BaseActivity 中,想用就直接使用了。
添加依赖:
event_bus: ^2.0.0
简单对 EventBus 封装一下,event_bus.dart 中代码:
import 'package:event_bus/event_bus.dart';
// 创建EventBus
class EventBusUtils {
static EventBus _instance;
static EventBus getInstance() {
if (null == _instance) {
_instance = new EventBus();
}
return _instance;
}
}
// event 监听
class MyEventBus {
//传递的数据
dynamic data;
//唯一标识码
int code;
MyEventBus({this.data, this.code});
}
basewidget.dart 中代码,之后所有的 widget 继承它就可以,对于 BaseWidget 可以看这里:?
Flutter 2进阶(七):BaseWidget的封装_wuqingsen1的博客-CSDN博客
import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'event_bus.dart';
abstract class BaseWidget extends StatefulWidget {
@override
BaseWidgetState createState() => getState();
BaseWidgetState getState();
}
abstract class BaseWidgetState<T extends BaseWidget> extends State<T> {
StreamSubscription myEventBus;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {}
@override
void dispose() {
super.dispose();
if (myEventBus != null) {
myEventBus.cancel();
}
}
}
为了方便看懂,很简单,大概流程:注册并发送广播,接收广播并用唯一标识码判断是哪个页面发送的并作出处理。
点击事件中发送广播:
onTap: () {
//头像点击,发广播
EventBusUtils.getInstance()
.fire(MyEventBus(code: EventCodeHeadClick, data: "发送广播"));
}
接收广播并处理:
@override
void initState() {
super.initState();
//接收广播
myEventBus = EventBusUtils.getInstance().on<MyEventBus>().listen((event) {
switch (event.code) {
case EventCodeHeadClick:
//点击头像,进入到我的页面
setState(() {
_controller.jumpToPage(3);
});
break;
}
});
}
上面我自定义了唯一标识码,constants 中代码存放全局变量:
///全局公用变量
const SPName = "name"; //SP存储姓名
const SPPassword = "password"; //SP存储密码
String USERNAME;
///广播序号
const EventCodeHeadClick = 0x100001; //首页点击头像
const EventCodeMy = 0x100002; //
轮播图插件:flutter_swiper?
这个就很简单了,依赖一加直接用:
flutter_swiper: ^1.1.6
使用:
import 'package:flutter/material.dart';
import 'package:flutter_blbl/model/base/stateful_widget_base.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
class HomeTabPage extends BaseWidget {
final String name;
HomeTabPage({this.name});
@override
_HomeTabPageState createState() => _HomeTabPageState();
@override
BaseWidgetState getState() {
return _HomeTabPageState();
}
}
class _HomeTabPageState extends BaseWidgetState<HomeTabPage> {
List<Image> imgs = [
Image.asset(
'images/banner_1.jpeg',
fit: BoxFit.cover,
),
Image.asset(
'images/banner_2.jpeg',
fit: BoxFit.cover,
),
Image.asset(
'images/banner_3.jpeg',
fit: BoxFit.cover,
),
];
@override
Widget build(BuildContext context) {
return Container(
child: Column(children: [
_getBanner(),
]),
);
}
//广告图
_getBanner() {
return Container(
width: MediaQuery.of(context).size.width,
height: 200.0,
child: Swiper(
itemBuilder: _swiperBuilder,
itemCount: imgs.length,
pagination: new SwiperPagination(
alignment: Alignment.bottomRight,
builder: DotSwiperPaginationBuilder(
color: Colors.grey,
activeColor: Colors.white,
size: 6,
activeSize: 6,
)),
scrollDirection: Axis.horizontal,
autoplay: true,
onTap: (index) => print('点击了第$index个'),
));
}
Widget _swiperBuilder(BuildContext context, int index) {
return InkWell(
onTap: () {},
child: Container(
padding: EdgeInsets.all(10),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(6)),
child: imgs[index]),
),
);
}
}
沉浸式状态栏插件:flutter_statusbar_manager?
依赖一加直接用:
flutter_statusbar_manager: ^2.0.0
封装一下:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
///沉浸式状态栏
enum StatusStytle { LIGHT_CONTENT, DARK_CONTENT } //明亮和黑暗
class MyNavigationBar extends StatelessWidget {
final StatusStytle statusStytle;
final Color color;
final double height;
final Widget child;
const MyNavigationBar(
{Key key,
this.statusStytle = StatusStytle.DARK_CONTENT,
this.color = Colors.white,
this.height = 46,
this.child})
: super(key: key);
@override
Widget build(BuildContext context) {
_statusBarInit();
//状态栏高度
var top = MediaQuery.of(context).padding.top;
return Container(
width: MediaQuery.of(context).size.width,
height: top + height,
child: child,
padding: EdgeInsets.only(top: top),
decoration: BoxDecoration(color: color),
);
}
void _statusBarInit() {
//沉浸式状态栏
FlutterStatusbarManager.setColor(color, animated: false);
FlutterStatusbarManager.setStyle(statusStytle == StatusStytle.DARK_CONTENT
? StatusBarStyle.DARK_CONTENT
: StatusBarStyle.LIGHT_CONTENT);
}
}
使用:
MyNavigationBar(
height: 50,
child: _appBar(),
color: Colors.white,
statusStytle: StatusStytle.DARK_CONTENT,
)
|