-
Notifications
You must be signed in to change notification settings - Fork 6
/
refresh_indicator_thread_home.dart
104 lines (97 loc) · 4.33 KB
/
refresh_indicator_thread_home.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
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
class RefreshIndicatorThreadHome extends StatefulWidget {
static const String routeName = 'refresh-indicator-thread-home';
@override
_RefreshIndicatorThreadHomeState createState() => _RefreshIndicatorThreadHomeState();
}
class _RefreshIndicatorThreadHomeState extends State<RefreshIndicatorThreadHome> {
StreamController<List<dynamic>> streamController = StreamController<List<dynamic>>();
Future<List<dynamic>> _getToDos() async {
const endpoint = 'https://jsonplaceholder.typicode.com/todos?userId=2';
print('Fetching from: $endpoint');
var response = await Dio().get(endpoint);
return response.data;
}
@override
void initState() {
_getToDos().then((todos) => streamController.add(todos));
super.initState();
}
@override
Widget build(BuildContext context) {
print('Rebuilt page!');
return Scaffold(
appBar: AppBar(
title: const Text('Refresh Indicator Example'),
),
body: RefreshIndicator(
onRefresh: () async => streamController.add(await _getToDos()),
child: StreamBuilder<List<dynamic>>(
stream: streamController.stream,
builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
if (snapshot.hasError) {
return const Center(child: Text('An Error Occurred'));
} else {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Center(child: CircularProgressIndicator());
case ConnectionState.active:
case ConnectionState.done:
return SingleChildScrollView(
clipBehavior: Clip.none,
physics: const AlwaysScrollableScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 20),
child: Text(
'Dummy Paragraph',
style: Theme.of(context).textTheme.headline5,
),
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 17),
child: Text(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut justo feugiat, sodales mauris eget, tristique metus. Aliquam mi tortor, fermentum nec viverra et, semper eget risus. Phasellus tincidunt suscipit erat a ullamcorper. In a mattis lectus. Mauris interdum condimentum ullamcorper. Sed eget porta risus, quis pulvinar lorem. Etiam porttitor aliquam diam, tristique mollis nisi ullamcorper eget.'),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 20),
child: Text(
'Checklist',
style: Theme.of(context).textTheme.headline5,
),
),
...List.generate(
3,
(i) => Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 10),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(width: 1, color: Theme.of(context).dividerColor)),
),
child: Row(
children: [
const Icon(Icons.check),
const SizedBox(width: 5),
Expanded(child: Text(snapshot.data![i]['title'])),
],
),
),
),
],
),
);
case ConnectionState.none:
default:
return const Center(child: Text('Nothing!'));
}
}
},
),
),
);
}
}