Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.21 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import '../models/Blogs.dart';
  3. import '../models/Blog.dart';
  4. import '../models/apiWorker.dart';
  5.  
  6. class BlogsPage extends StatefulWidget {
  7.   BlogsPage({Key key}) : super(key: key);
  8.  
  9.   _BlogsPageState createState() => _BlogsPageState();
  10. }
  11.  
  12. class _BlogsPageState extends State<BlogsPage>
  13. {
  14.   apiWorker _api = apiWorker();
  15.  
  16.   Future<Blogs> _loadBlogs() async {
  17.  
  18.     return await _api.getBlogs();
  19.   }
  20.  
  21.   Widget build (BuildContext context) {
  22.     return Container(
  23.         alignment: Alignment.topCenter,
  24.         child: FutureBuilder<Blogs>(
  25.             future: _loadBlogs(),
  26.             builder: (BuildContext context, AsyncSnapshot snapshot) {
  27.               switch (snapshot.connectionState) {
  28.                 case ConnectionState.none:
  29.                 case ConnectionState.waiting:
  30.                   return Center(child: CircularProgressIndicator());
  31.                 default:
  32.                   if (snapshot.hasError)
  33.                     return new Text('Ошибка: ${snapshot.error}');
  34.                   else
  35.                     return createListView(context, snapshot);
  36.               }
  37.             }
  38.         ),
  39.     );
  40.   }
  41.  
  42.     Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
  43.  
  44.               List<Blog> values = snapshot.data.blogs;
  45.               return new ListView.builder(
  46.               itemCount: values.length,
  47.               itemBuilder: (BuildContext context, int index) {
  48.                   return Column(
  49.                     children: <Widget>[
  50.                       ListTile(
  51.                         title: Text(values[index].name),
  52.                         subtitle: Text("http://${values[index].uri}.tumblr.com",
  53.                           style: TextStyle(
  54.                             fontSize: 12,
  55.                           ),
  56.                         ),
  57.                         trailing: IconButton(
  58.                             icon: Icon(Icons.delete),
  59.                             onPressed: () {
  60.                               _deleteBlog(values[index].id, values[index].name);
  61.                             },
  62.                             ),
  63.                       ),
  64.                       Divider(height: 3),
  65.                     ],
  66.                   );
  67.  
  68.                 },
  69.               );
  70.   }
  71.  
  72.   void _deleteBlog(String id, String name) {
  73.     showDialog(
  74.       context: context,
  75.       barrierDismissible: false,
  76.       builder: (BuildContext context) {
  77.         return AlertDialog(
  78.           title: Text("Вы хотите удалить блог?"),
  79.           content: Text("Вы точно хотите удалить \"${name}\"?"),
  80.           actions: <Widget>[
  81.             FlatButton(
  82.               child: Text("Удалить"),
  83.               onPressed: () {
  84.                 setState(() {
  85.  
  86.                 });
  87.               },
  88.             ),
  89.             FlatButton(
  90.               child: Text("Отмена"),
  91.               onPressed: () {
  92.                 setState(() {
  93.                   _deleteApi(id);
  94.                 });
  95.  
  96.                 Navigator.pop(context);
  97.               },
  98.             )
  99.           ],
  100.         );
  101.       }
  102.  
  103.     );
  104.   }
  105.  
  106.   void _deleteApi(id) async {
  107.     await _api.deleteBlog(id);
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement