Twitter - Api half way there - have to figure out the Twitter Authorization with Dart.

This commit is contained in:
kwainright
2020-04-08 19:17:24 -04:00
parent b8b0a790f6
commit a2e2cb693d
3 changed files with 118 additions and 12 deletions

View File

@@ -1,6 +1,10 @@
import 'package:capstone_hungry_hippos/twitter_assets/twitterFeed.dart';
import 'package:flutter/material.dart';
import '../twitter_assets/tweet.dart';
class TwitterFeed extends StatelessWidget{
final feed = TwitterFeedCreation();
@override
Widget build(BuildContext context) {
return StatefulBuilder(
@@ -12,18 +16,7 @@ class TwitterFeed extends StatelessWidget{
),
body: Container(
color: Colors.black12,
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, ind){
return Card(
child: ListTile(
leading: FlutterLogo(),
title: Text('Twitter User'),
subtitle: Text('Tweet: I really hope I can figure out the whole URl thing what if this is longer and stuff. will it wrap around? I hope so we about to find out'),
),
);
}
),
child: VerticalTwitterFeed(twitterFeed: feed),
),
drawer: Drawer(
child: ListView(

View File

@@ -0,0 +1,53 @@
import 'package:flutter/material.dart';
class Tweet {
final int id;
final String text;
final String userName;
final String userImgUrl;
final String url;
Tweet(
this.id, {
this.text,
this.userName,
this.userImgUrl,
this.url,
});
factory Tweet.fromJson(Map<String, dynamic> json) {
return Tweet(
json['id_str'],
text: json['text'],
userName: json['user']['name'],
userImgUrl: json['user']['profile_image_url_https'],
url: json['urls']['expanded_url'],
);
}
}
class TwitterCard extends StatelessWidget{
const TwitterCard({
Key key,
@required this.tweet,
}) : super(key: key);
final Tweet tweet;
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: SizedBox(
width: 75,
height: 75,
child: Image.network('${tweet.userImgUrl}'),
),
title: Text('${tweet.userName}'),
subtitle: Text('${tweet.text}'),
),
);
}
}

View File

@@ -0,0 +1,60 @@
import 'dart:io';
import 'package:capstone_hungry_hippos/screens/twitter_widget.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'tweet.dart';
class TwitterFeedCreation {
static final twitterBase = 'https://api.twitter.com/1.1/search';
static final apiKey = 'AFBfOqx8uXIUBZMxAFoQyO3zA';
static final apiSecret = '48Gp7nczz9SqExorwYuWpA6Nmviuox6Beq83kjH1XtYtunorym';
Future<List<Tweet>> getPage({int size = 10}) async {
var url = '$twitterBase/tweets.json?q=from%3ACharlotteFTBL&result_type=mixed&count=$size';
http.Response response = await http.get(url
);
Iterable tweets = json.decode(response.body);
return tweets.map((e) => Tweet.fromJson(e)).toList();
}
}
class VerticalTwitterFeed extends StatelessWidget{
final TwitterFeedCreation twitterFeed;
const VerticalTwitterFeed({
Key key,
@required this.twitterFeed,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder(
future: twitterFeed.getPage(),
builder: (ctx, snapshot){
if(!snapshot.hasData){
return Center(child: CircularProgressIndicator());
}else{
List<Tweet> tweets = snapshot.data;
return ListView.builder(
scrollDirection: Axis.vertical,
itemCount: tweets.length,
itemBuilder: (ctx, idx) {
return TwitterCard(
tweet: tweets[idx],
);
},
);
}
},
),
);
}
}