This repository has been archived on 2023-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
meincantor-app/lib/schuelerzeitung.dart

107 lines
3.7 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:MeinCantor/networking.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2021-11-19 20:12:42 +01:00
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:http/http.dart' as http;
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
class SZ extends StatelessWidget {
const SZ({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Schülerzeitung"),
centerTitle: true,
),
body: FutureBuilder<http.Response>(
future: getArticles(),
builder: (context, snapshot) {
if (snapshot.hasData) {
int statusCode = snapshot.data!.statusCode;
if (statusCode == 200) {
String data = utf8.decode(snapshot.data!.bodyBytes);
List articles = jsonDecode(data);
List<Widget> articleTiles = [];
for (var element in articles) {
Card card = Card(
child: Column(children: [
Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
child: ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Article.fromData(element["title"], element["content"], element["author"], element["published_at"]).widget),
);
},
title: Text(element["title"],
style: const TextStyle(fontWeight: FontWeight.bold)),
subtitle: Text(
element["summary"])),
)
]),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
);
articleTiles.add(card);
}
return ListView(
children: articleTiles.reversed.toList(),
);
} else {
return(const Center(
child: Text("Uups... Irgendwas ist schief gelaufen")
));
}
} else {
return(const Center(
child: CircularProgressIndicator()
));
}
},
),
);
}
}
class Article {
Widget widget;
//const Article({Key? key}) : super(key: key);
Article({required this.widget});
factory Article.fromData(String title, String content, String author, String publishDate, ) {
return Article(widget: Scaffold(
appBar: AppBar(
title: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text(title)
),
),
centerTitle: true,
),
body: ListView(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
children: [
ListTile(
2021-11-19 20:12:42 +01:00
leading: const Icon(MdiIcons.accountOutline),
title: Text(author),
),
ListTile(
2021-11-19 20:12:42 +01:00
leading: const Icon(MdiIcons.calendarOutline),
title: Text("${DateTime.parse(publishDate).day.toString()}.${DateTime.parse(publishDate).month.toString()}.${DateTime.parse(publishDate).year.toString()}"),
),
2021-11-19 20:12:42 +01:00
MarkdownBody(data: content)
],
)
)
);
}
}