Skip to content

Instantly share code, notes, and snippets.

@jargnar
Last active March 13, 2021 12:02
Show Gist options
  • Save jargnar/edbac4ea3c98d561b34ce9f7140b5154 to your computer and use it in GitHub Desktop.
Save jargnar/edbac4ea3c98d561b34ce9f7140b5154 to your computer and use it in GitHub Desktop.
Android/Flutter simple news feed sim
import 'dart:typed_data';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:intl/intl.dart';
class ArticleModel {
String title;
String body;
Uint8List img;
Timestamp dt;
ArticleModel(this.title, this.body, this.img, this.dt);
ArticleModel.fromDocumentSnapshot(DocumentSnapshot doc) {
this.title = doc.data()['title'];
this.body = doc.data()['body'];
this.img = base64Decode(doc.data()['img']);
this.dt = doc.data()['dt'];
}
}
Container articleImageHeaderView(MemoryImage img) {
return Container(
height: 200,
decoration:
BoxDecoration(image: DecorationImage(image: img, fit: BoxFit.fill)),
);
}
Container articleBodyView(String body) {
return Container(
child: Text(body,
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w400)),
padding: EdgeInsets.all(10.0),
);
}
Container articleTitleView(String title) {
return Container(
child: Text(title,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 28, fontWeight: FontWeight.w700)),
padding: EdgeInsets.all(10.0),
);
}
Container articleDateView(Timestamp ts) {
DateFormat format = new DateFormat("EEEE, MMMM d yyyy");
String dt = format.format(ts.toDate());
return Container(
child: Text(dt,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500)),
padding: EdgeInsets.all(10.0),
);
}
SingleChildScrollView articlePageViewContainer(
ArticleModel article, BuildContext context) {
return SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width,
child: Column(children: [
articleImageHeaderView(MemoryImage(article.img)),
articleTitleView(article.title),
articleDateView(article.dt),
articleBodyView(article.body)
])));
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(home: Splash());
}
class Splash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(body: Article());
}
}
class Article extends StatelessWidget {
@override
Widget build(BuildContext context) {
Query users = FirebaseFirestore.instance
.collection('news')
.orderBy('dt', descending: true)
.limit(10);
return StreamBuilder<QuerySnapshot>(
stream: users.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) return Text('Something went wrong');
if (snapshot.connectionState == ConnectionState.waiting)
return Text("Loading");
var article = snapshot.data.docs
.map((doc) => new ArticleModel.fromDocumentSnapshot(doc))
.toList();
return new PageView(
children: article
.map((a) => articlePageViewContainer(a, context))
.toList());
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment