· 6 years ago · Oct 07, 2019, 08:10 PM
1import 'dart:convert';
2
3import 'package:flutter/foundation.dart';
4import 'package:flutter/material.dart';
5import 'package:http/http.dart' as http;
6import 'dart:async';
7
8const baseUrl = "https://jsonplaceholder.typicode.com";
9
10class API {
11 static Future getUsers() {
12 var url = baseUrl + "/posts";
13 return http.get(url);
14 }
15}
16
17class Todo {
18 int id;
19 String title;
20 String description;
21
22// Todo(this.title, this.description);
23 Todo(int id, String name, String body) {
24 this.id = id;
25 this.title = name;
26 this.description = body;
27 }
28
29 Todo.fromJson(Map json)
30 : id = json['id'],
31 title = json['title'],
32 description = json['body'];
33
34 Map toJson() {
35 return {'id': id, 'title': title, 'body': description};
36 }
37}
38
39void main() => runApp(MyApp());
40
41class MyApp extends StatelessWidget {
42 @override
43 build(context) {
44 return MaterialApp(
45 debugShowCheckedModeBanner: false,
46 title: 'My Http App',
47 theme: ThemeData(
48 primarySwatch: Colors.blue,
49 ),
50 home: MyListScreen(),
51 );
52 }
53}
54
55class MyListScreen extends StatefulWidget {
56 @override
57 createState() => _MyListScreenState();
58}
59
60class _MyListScreenState extends State {
61 var todos = new List<Todo>();
62
63 _getUsers() {
64 API.getUsers().then((response) {
65 setState(() {
66 Iterable list = json.decode(response.body);
67 todos = list.map((model) => Todo.fromJson(model)).toList();
68 });
69 });
70 }
71
72 initState() {
73 super.initState();
74 _getUsers();
75 }
76
77 dispose() {
78 super.dispose();
79 }
80
81 @override
82 build(context) {
83 return Scaffold(
84 appBar: AppBar(
85 title: Text("User List"),
86 ),
87 body: ListView.builder(
88 itemCount: todos.length,
89 itemBuilder: (context, index) {
90 return ListTile(
91 title: Text(todos[index].title),
92 onTap: () {
93 Navigator.push(
94 context,
95 MaterialPageRoute(
96 builder: (context) => DetailScreen(todo: todos[index]),
97 ),
98 );
99 },
100 );
101 },
102 ));
103 }
104
105 void removeItem(int index) {
106 setState(() {
107 todos = List.from(todos)
108 ..removeAt(index);
109 });
110 }
111}
112
113class DetailScreen extends StatelessWidget {
114 // Declare a field that holds the Todo.
115 final Todo todo;
116
117 // In the constructor, require a Todo.
118 DetailScreen({Key key, @required this.todo}) : super(key: key);
119
120 @override
121 Widget build(BuildContext context) {
122 // Use the Todo to create the UI.
123 return Scaffold(
124 appBar: AppBar(
125 title: Text(todo.title),
126 ),
127 body: Center(
128 child: Column(
129 mainAxisAlignment: MainAxisAlignment.center,
130 children: <Widget>[
131 Padding(
132 padding: EdgeInsets.all(16.0),
133 child: Text(todo.description),
134 ),
135 Padding( // Эта кнопка должна показываться изначально после клика по ней скрывается и показывается кнопка "Закончить"
136 padding: const EdgeInsets.all(8.0),
137 child: RaisedButton(
138 onPressed: () {
139 },
140 child: Text('Начать'),
141 ),
142 ),
143 Padding( // Эта кнопка должна быть скрыта и показываться только после нажатия кнопки "Начать". Так же необходимо удалить из Listview нужный элемент
144 padding: const EdgeInsets.all(8.0),
145 child: RaisedButton(
146 onPressed: () {
147 // Pop here with "Nope"
148 Navigator.pop(context, todo.id);
149 },
150 child: Text('Закончить'),
151 ),
152 )
153 ],
154 ),
155 ),
156 );
157 }
158}