forked from vandadnp/flutter-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reusable-separators-in-flutter.dart
80 lines (72 loc) · 2.21 KB
/
reusable-separators-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
// 🐦 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:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rxdart/rxdart.dart';
void main() {
runApp(
const App(),
);
}
class App extends StatelessWidget {
const App({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
Stream<String> getNames({
required String filePath,
}) {
final catNames = rootBundle.loadString(filePath);
return Stream.fromFuture(catNames).transform(const LineSplitter());
}
Stream<String> getAllNames() => getNames(filePath: 'texts/cats.txt')
.concatWith([getNames(filePath: 'texts/dogs.txt')]);
Widget separatorBuilder(_, __) => const Divider(
color: Colors.black,
);
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<List<String>>(
future: getAllNames().toList(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.done:
final names = snapshot.requireData;
return ListView.separated(
separatorBuilder: separatorBuilder,
itemCount: names.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(names[index]),
);
},
);
default:
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
}