-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream_provider_screen.dart
53 lines (46 loc) · 1.23 KB
/
stream_provider_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:provider/provider.dart';
class StreamProviderScreen extends StatefulWidget {
@override
_StreamProviderScreenState createState() => _StreamProviderScreenState();
}
class _StreamProviderScreenState extends State<StreamProviderScreen> {
final controller = StreamController<bool>();
@override
void dispose() {
controller.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('StreamProvider Example'),
),
body: StreamProvider.value(
value: controller.stream,
initialData: false,
child: HomePage(controller: controller),
),
);
}
}
class HomePage extends StatelessWidget {
final StreamController<bool> controller;
HomePage({this.controller});
@override
Widget build(BuildContext context) {
bool isLogin = Provider.of<bool>(context);
return Container(
color: isLogin ? Colors.green : Colors.grey,
alignment: Alignment.center,
child: MaterialButton(
child: Text(isLogin ? "登出" : "登入"),
onPressed: () {
controller.add(!isLogin);
},
),
);
}
}