Created
August 28, 2019 16:54
-
-
Save shihaohong/0ccf6e6d6cdc1334765e5d2845b21919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
void main() => runApp(MyApp()); | |
Future<Post> fetchPost() async { | |
final response = await http.get('https://jsonplaceholder.typicode.com/posts/1'); | |
if (response.statusCode == 200) { | |
return Post.fromJson(json.decode(response.body)); | |
} else { | |
throw Exception('Failed to load post'); | |
} | |
} | |
class Post { | |
final int userId; | |
final int id; | |
final String title; | |
final String body; | |
Post({this.userId, this.id, this.title, this.body}); | |
factory Post.fromJson(Map<String, dynamic> json) { | |
return Post( | |
userId: json['userId'], | |
id: json['id'], | |
title: json['title'], | |
body: json['body'], | |
); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { | |
TabController _controller; | |
List<Post> menuCategoryList = []; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = TabController(vsync: this, length: menuCategoryList.length); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
bottom: TabBar( | |
controller: _controller, | |
tabs: menuCategoryList.map((Post post) { | |
return Tab(child: Text(post.title)); | |
}).toList(), | |
), | |
), | |
body: TabBarView( | |
controller: _controller, | |
children: menuCategoryList.map((Post post) { | |
final String label = post.title.toLowerCase(); | |
return Center( | |
child: Text( | |
'This is the $label tab', | |
style: const TextStyle(fontSize: 36), | |
), | |
); | |
}).toList(), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
fetchPost() | |
.then((Post post) { | |
setState(() { | |
menuCategoryList = new List<Post>.from(menuCategoryList) | |
..add(post); | |
_controller = TabController(vsync: this, length: menuCategoryList.length); | |
}); | |
}); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment