Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import '../models/Blogs.dart';
- import '../models/Blog.dart';
- import '../models/apiWorker.dart';
- class BlogsPage extends StatefulWidget {
- BlogsPage({Key key}) : super(key: key);
- _BlogsPageState createState() => _BlogsPageState();
- }
- class _BlogsPageState extends State<BlogsPage>
- {
- apiWorker _api = apiWorker();
- Future<Blogs> _loadBlogs() async {
- return await _api.getBlogs();
- }
- Widget build (BuildContext context) {
- return Container(
- alignment: Alignment.topCenter,
- child: FutureBuilder<Blogs>(
- future: _loadBlogs(),
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- switch (snapshot.connectionState) {
- case ConnectionState.none:
- case ConnectionState.waiting:
- return Center(child: CircularProgressIndicator());
- default:
- if (snapshot.hasError)
- return new Text('Ошибка: ${snapshot.error}');
- else
- return createListView(context, snapshot);
- }
- }
- ),
- );
- }
- Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
- List<Blog> values = snapshot.data.blogs;
- return new ListView.builder(
- itemCount: values.length,
- itemBuilder: (BuildContext context, int index) {
- return Column(
- children: <Widget>[
- ListTile(
- title: Text(values[index].name),
- subtitle: Text("http://${values[index].uri}.tumblr.com",
- style: TextStyle(
- fontSize: 12,
- ),
- ),
- trailing: IconButton(
- icon: Icon(Icons.delete),
- onPressed: () {
- _deleteBlog(values[index].id, values[index].name);
- },
- ),
- ),
- Divider(height: 3),
- ],
- );
- },
- );
- }
- void _deleteBlog(String id, String name) {
- showDialog(
- context: context,
- barrierDismissible: false,
- builder: (BuildContext context) {
- return AlertDialog(
- title: Text("Вы хотите удалить блог?"),
- content: Text("Вы точно хотите удалить \"${name}\"?"),
- actions: <Widget>[
- FlatButton(
- child: Text("Удалить"),
- onPressed: () {
- setState(() {
- });
- },
- ),
- FlatButton(
- child: Text("Отмена"),
- onPressed: () {
- setState(() {
- _deleteApi(id);
- });
- Navigator.pop(context);
- },
- )
- ],
- );
- }
- );
- }
- void _deleteApi(id) async {
- await _api.deleteBlog(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement