Flutter 知识点集结
Flutter生命周期
一个StatefulWidget的生命周期
APP进入前后台
WidgetsBindingObserver
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// went to Background
}
if (state == AppLifecycleState.resumed) {
// came back to Foreground
}
}
Flutter Stream
根据英文付费文章阅读得到的整理
简介
Stream和Future的比较
- 相同点
- Both works asynchronously(异步)
- Both have some potential(潜在) value
- 不同点
- A stream is a combination(集合) of Futures
- Future has only one response but Stream could have any number of Response.(Future只有一个响应,Stream可以有任意数量的响应)
Stream的类型
- Single Subscription: There could be a maximum of one listener to this stream.(单一订阅,只能有一个监听者)
- Broadcast: There could be the infinite number of the listener to this stream.(广播,无数监听者)
Stream 示例
示例1: Stream .periodic
// 每隔2秒调用一下callBack,
// 如果没传callback,传的是null,则每隔2秒会打印 null
void main() async {
Duration interval = Duration(seconds: 2);
Stream<int> stream = Stream<int>.periodic(interval, callback);
await for(int i in stream){
print(i);
}
}
int callback(int value) {
return (value+1)*2;
}
示例2: Stream .take
上个示例并不会停止,想要停止,可以加上Stream.take
// 打印5次结束
void main() async {
Duration interval = Duration(seconds: 2);
Stream<int> stream = Stream<int>.periodic(interval, callback);
stream = stream.take(5);
await for(int i in stream){
print(i);
}
}
int callback(int value) {
return (value+1)*2;
}
示例3: Stream .takeWhile
如果想要满足某种条件才结束,Stream .take换成Stream .takeWhile
// 当值到20的时候结束
void main() async {
Duration interval = Duration(seconds: 2);
Stream<int> stream = Stream<int>.periodic(interval, callback);
stream = stream.takeWhile(condition);
await for(int i in stream){
print(i);
}
}
int callback(int value) {
return (value+1)*2;
}
bool condition(int x) {
return x <= 20;
}
示例4: Stream .skip
跳过前几次的callBack调用
// 打印10次,跳过前2次,
void main() async {
Duration interval = Duration(seconds: 2);
Stream<int> stream = Stream<int>.periodic(interval, callback);
stream = stream.takeWhile(condition);
await for(int i in stream){
print(i);
}
}
int callback(int value) {
return (value+1)*2;
}
bool condition(int x) {
return x <= 20;
}
示例5: Stream .skipWhile
跳过满足条件的callBack调用,一旦不满足,后面就不会再检查了
// 打印10次,过滤掉小于5的数,
// 6 8 10 12 14 16 18 20
main() async {
Duration interval = Duration(seconds: 1);
Stream<int> stream = Stream<int>.periodic(interval,transform);
stream = stream.take(10);
stream = stream.skipWhile(condition);
await for(int i in stream){
print(i);
}
}
int transform(int x){
return (x + 1) * 2;
}
bool condition(int x){
return x < 5;
}
示例6: Stream .toList
将stream流中的数据转换成list数组
// 2 4 6 8 10
main() async {
Duration interval = Duration(seconds: 1);
Stream<int> stream = Stream<int>.periodic(interval,transform);
stream = stream.take(5);
List<int> data = await stream.toList(); // 转成数组,这里会一次性得到2 4 6 8 10的数组,而直接从stream拿值是有一个拿一个
for(int i in data){
print(i);
}
}
int transform(int x){
return (x + 1) * 2;
}
示例7: Stream .forEach
除了用listen,也可以用forEach
// 2 4 6 8 10
main() async {
Duration interval = Duration(seconds: 1);
Stream<int> stream = Stream<int>.periodic(interval,transform);
stream = stream.take(5);
stream.forEach((int x){
print(x);
});
}
int transform(int x){
return (x + 1) * 2;
}
Stream 属性
Stream .forEach
获取Stream发出的所有事件的数量,和toList一样,他是需要等待流发出所有事情后才触发
StreamBuilder
StreamBuilder listen to the stream and build itself on every new emitted Event by the stream. (StreamBuilder会监听每次stream发出的事件)
StreamBuilder和Stream联合使用
即使1分钟停止
/import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: HomePage(),
title: 'Stream Demo',
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stream Demo'),
),
body: Center(
child: StreamBuilder(
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(
'1 Minute Completed',
style: TextStyle(
fontSize: 30.0,
),
);
} else if (snapshot.connectionState == ConnectionState.waiting) {
return Text(
'Waiting For Stream',
style: TextStyle(
fontSize: 30.0,
),
);
}
return Text(
'00:${snapshot.data.toString().padLeft(2,'0')}',
style: TextStyle(
fontSize: 30.0,
),
);
},
initialData: 0,
stream: _stream(),
),
),
);
}
Stream<int> _stream() {
Duration interval = Duration(seconds: 1);
Stream<int> stream = Stream<int>.periodic(interval, transform);
stream = stream.take(59);
return stream;
}
int transform(int value) {
return value;
}
}
Notification
是从子向父类传递数据的一种方式
使用方法
- 定义一个class继承Notification
class TableViewNotifier extends Notification {
int scrollSection;
TableViewNotifier(this.scrollSection);
}
- 子类发送通知给上级
TableViewNotifier(this.currentHeaderModel.section).dispatch(context);
- 上级监听数据改变
用NotificationListener包住要监听的上级widget
NotificationListener<TableViewNotifier> (
onNotification: (notification) {
choseSection = notification.scrollSection;
setState(() {
});
return true;
},
child: your widget