-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
streamqueue-in-flutter.dart
133 lines (122 loc) · 3.56 KB
/
streamqueue-in-flutter.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:flutter/services.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
debugShowCheckedModeBanner: false,
),
);
}
const images = [
'https://bit.ly/3x7J5Qt',
'https://bit.ly/3ywI8l6',
'https://bit.ly/3jOueGG',
'https://bit.ly/3qYOtDm',
'https://bit.ly/3wt11Ec',
'https://bit.ly/3yvFg7X',
'https://bit.ly/3ywzOla',
'https://bit.ly/3wnASpW',
'https://bit.ly/3jXSDto',
];
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
Stream<Uint8List> getImagesData() async* {
for (final url in images) {
final bundle = NetworkAssetBundle(Uri.parse(url));
final loaded = await bundle.load(url);
final bytes = loaded.buffer.asUint8List();
yield bytes;
}
}
class _HomePageState extends State<HomePage> {
late final StreamQueue<Uint8List> queue;
final List<Uint8List> dataArray = [];
bool isLoading = false;
@override
void initState() {
queue = StreamQueue<Uint8List>(getImagesData());
super.initState();
}
@override
void dispose() {
queue.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: queue.hasNext,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
return Scaffold(
appBar: AppBar(
title: const Text('StreamQueue in Flutter'),
actions: [
Visibility(
visible: snapshot.requireData,
child: IconButton(
onPressed: () async {
setState(() {
isLoading = true;
});
final imageData = await queue.next;
setState(() {
isLoading = false;
dataArray.add(imageData);
});
},
icon: const Icon(
Icons.add,
),
),
),
],
leading: Visibility(
visible: isLoading,
child: const CircularProgressIndicator(),
),
),
body: GridView.builder(
padding: const EdgeInsets.all(8.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
),
itemCount: dataArray.length,
itemBuilder: (context, index) {
final imageData = dataArray[index];
return Image.memory(
imageData,
fit: BoxFit.cover,
);
},
),
);
},
);
}
}