CitizenForum done. Navbar working great. Introduced cache for firestore to hopefully don't reach the bandwith limit again
This commit is contained in:
+18
-5
@@ -45,13 +45,26 @@ class _AddScreenState extends State<AddScreen> {
|
|||||||
|
|
||||||
|
|
||||||
// Das neue Element in der Firestore-Sammlung "Issues" speichern
|
// Das neue Element in der Firestore-Sammlung "Issues" speichern
|
||||||
if(_selectedIndex == 1)
|
if(_selectedIndex == 0)
|
||||||
{
|
{
|
||||||
Reference ref = FirebaseStorage.instance.ref().child("Issues/${DateTime.now().toString()}");
|
Reference ref = FirebaseStorage.instance.ref().child("CitizensForum/${DateTime.now().toString()}");
|
||||||
UploadTask uploadTask = ref.putFile(imageFile);
|
UploadTask uploadTask = ref.putFile(imageFile);
|
||||||
String imageUrl = await (await uploadTask).ref.getDownloadURL();
|
String imageUrl = await (await uploadTask).ref.getDownloadURL();
|
||||||
|
|
||||||
await FirebaseFirestore.instance.collection("Issues").add({
|
await FirebaseFirestore.instance.collection("CitizensForum").add({
|
||||||
|
"title": title,
|
||||||
|
"description": description,
|
||||||
|
"imageUrl": imageUrl,
|
||||||
|
"createdDate": DateTime.now(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(_selectedIndex == 1)
|
||||||
|
{
|
||||||
|
Reference ref = FirebaseStorage.instance.ref().child("News/${DateTime.now().toString()}");
|
||||||
|
UploadTask uploadTask = ref.putFile(imageFile);
|
||||||
|
String imageUrl = await (await uploadTask).ref.getDownloadURL();
|
||||||
|
|
||||||
|
await FirebaseFirestore.instance.collection("News").add({
|
||||||
"title": title,
|
"title": title,
|
||||||
"description": description,
|
"description": description,
|
||||||
"imageUrl": imageUrl,
|
"imageUrl": imageUrl,
|
||||||
@@ -60,10 +73,10 @@ class _AddScreenState extends State<AddScreen> {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Reference ref = FirebaseStorage.instance.ref().child("News/${DateTime.now().toString()}");
|
Reference ref = FirebaseStorage.instance.ref().child("Issues/${DateTime.now().toString()}");
|
||||||
UploadTask uploadTask = ref.putFile(imageFile);
|
UploadTask uploadTask = ref.putFile(imageFile);
|
||||||
String imageUrl = await (await uploadTask).ref.getDownloadURL();
|
String imageUrl = await (await uploadTask).ref.getDownloadURL();
|
||||||
await FirebaseFirestore.instance.collection("News").add({
|
await FirebaseFirestore.instance.collection("Issues").add({
|
||||||
"title": title,
|
"title": title,
|
||||||
"description": description,
|
"description": description,
|
||||||
"imageUrl": imageUrl,
|
"imageUrl": imageUrl,
|
||||||
|
|||||||
+196
-13
@@ -93,10 +93,6 @@ class UserDefinedItem extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class NewsList extends StatefulWidget {
|
class NewsList extends StatefulWidget {
|
||||||
const NewsList({Key? key}) : super(key: key);
|
const NewsList({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@@ -104,6 +100,193 @@ class NewsList extends StatefulWidget {
|
|||||||
_NewsListState createState() => _NewsListState();
|
_NewsListState createState() => _NewsListState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _NewsListState extends State<NewsList> {
|
||||||
|
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||||
|
late Stream<QuerySnapshot> newsStream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
// Enable persistence
|
||||||
|
firestore.enablePersistence();
|
||||||
|
|
||||||
|
// Enable network
|
||||||
|
firestore.settings = Settings(cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);
|
||||||
|
|
||||||
|
// Configure the stream to use the cache first and then the server
|
||||||
|
newsStream = firestore
|
||||||
|
.collection('News')
|
||||||
|
.orderBy('createdDate', descending: true)
|
||||||
|
.snapshots(includeMetadataChanges: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return StreamBuilder<QuerySnapshot>(
|
||||||
|
stream: newsStream,
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
|
||||||
|
if (snapshot.hasError) {
|
||||||
|
return Text('Error: ${snapshot.error}');
|
||||||
|
}
|
||||||
|
switch (snapshot.connectionState) {
|
||||||
|
case ConnectionState.waiting:
|
||||||
|
return const Text('Loading...');
|
||||||
|
default:
|
||||||
|
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: documents.length,
|
||||||
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
final Map<String, dynamic> data =
|
||||||
|
documents[index].data()! as Map<String, dynamic>;
|
||||||
|
final DateTime createdDate =
|
||||||
|
data['createdDate'].toDate();
|
||||||
|
final formattedDate =
|
||||||
|
DateFormat('dd.MM.yyyy').format(createdDate);
|
||||||
|
return UserDefinedItem(
|
||||||
|
title: data['title'] ?? '',
|
||||||
|
datum: formattedDate,
|
||||||
|
description: data['description'] ?? '',
|
||||||
|
image: Image.network((data['imageUrl'])),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class IssueList extends StatefulWidget {
|
||||||
|
const IssueList({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_IssueListState createState() => _IssueListState();
|
||||||
|
}
|
||||||
|
class _IssueListState extends State<IssueList> {
|
||||||
|
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||||
|
late Stream<QuerySnapshot> issueStream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
// Enable persistence
|
||||||
|
firestore.enablePersistence();
|
||||||
|
|
||||||
|
// Enable network
|
||||||
|
firestore.settings = Settings(cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);
|
||||||
|
|
||||||
|
// Configure the stream to use the cache first and then the server
|
||||||
|
issueStream = firestore
|
||||||
|
.collection('Issues')
|
||||||
|
.orderBy('createdDate', descending: true)
|
||||||
|
.snapshots(includeMetadataChanges: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return StreamBuilder<QuerySnapshot>(
|
||||||
|
stream: issueStream,
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
|
||||||
|
if (snapshot.hasError) {
|
||||||
|
return Text('Error: ${snapshot.error}');
|
||||||
|
}
|
||||||
|
switch (snapshot.connectionState) {
|
||||||
|
case ConnectionState.waiting:
|
||||||
|
return const Text('Loading...');
|
||||||
|
default:
|
||||||
|
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: documents.length,
|
||||||
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
final Map<String, dynamic> data =
|
||||||
|
documents[index].data()! as Map<String, dynamic>;
|
||||||
|
final DateTime createdDate =
|
||||||
|
data['createdDate'].toDate();
|
||||||
|
final formattedDate =
|
||||||
|
DateFormat('dd.MM.yyyy').format(createdDate);
|
||||||
|
return UserDefinedItem(
|
||||||
|
title: data['title'] ?? '',
|
||||||
|
datum: formattedDate,
|
||||||
|
description: data['description'] ?? '',
|
||||||
|
image: Image.network((data['imageUrl'])),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class CitizensForum extends StatefulWidget {
|
||||||
|
const CitizensForum({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_CitizensForumState createState() => _CitizensForumState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CitizensForumState extends State<CitizensForum> {
|
||||||
|
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||||
|
late Stream<QuerySnapshot<Map<String, dynamic>>> citizenStream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
// Enable persistence
|
||||||
|
firestore.enablePersistence();
|
||||||
|
|
||||||
|
// Enable network
|
||||||
|
firestore.settings = Settings(cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);
|
||||||
|
|
||||||
|
// Configure the stream to use the cache first and then the server
|
||||||
|
citizenStream = firestore
|
||||||
|
.collection('CitizensForum')
|
||||||
|
.orderBy('createdDate', descending: true)
|
||||||
|
.snapshots(includeMetadataChanges: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
|
||||||
|
stream: citizenStream,
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
|
||||||
|
if (snapshot.hasError) {
|
||||||
|
return Text('Error: ${snapshot.error}');
|
||||||
|
}
|
||||||
|
switch (snapshot.connectionState) {
|
||||||
|
case ConnectionState.waiting:
|
||||||
|
return const Text('Loading...');
|
||||||
|
default:
|
||||||
|
final List<QueryDocumentSnapshot<Map<String, dynamic>>> documents = snapshot.data!.docs;
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: documents.length,
|
||||||
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
final Map<String, dynamic> data = documents[index].data();
|
||||||
|
final DateTime createdDate = data['createdDate'].toDate();
|
||||||
|
final formattedDate = DateFormat.yMd().format(createdDate);
|
||||||
|
return UserDefinedItem(
|
||||||
|
title: data['title'] ?? '',
|
||||||
|
datum: formattedDate,
|
||||||
|
description: data['description'] ?? '',
|
||||||
|
image: Image.network((data['imageUrl'])),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*class NewsList extends StatefulWidget {
|
||||||
|
const NewsList({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_NewsListState createState() => _NewsListState();
|
||||||
|
}
|
||||||
|
|
||||||
class _NewsListState extends State<NewsList> {
|
class _NewsListState extends State<NewsList> {
|
||||||
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||||
late Stream<QuerySnapshot> newsStream;
|
late Stream<QuerySnapshot> newsStream;
|
||||||
@@ -201,12 +384,12 @@ class CitizensForum extends StatefulWidget {
|
|||||||
const CitizensForum({Key? key}) : super(key: key);
|
const CitizensForum({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_CitizensForum createState() => _CitizensForum();
|
_CitizensForumState createState() => _CitizensForumState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _CitizensForum extends State<IssueList> {
|
class _CitizensForumState extends State<CitizensForum> {
|
||||||
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||||
late Stream<QuerySnapshot> citizenStream;
|
late Stream<QuerySnapshot<Map<String, dynamic>>> citizenStream;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -216,9 +399,9 @@ class _CitizensForum extends State<IssueList> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return StreamBuilder<QuerySnapshot>(
|
return StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
|
||||||
stream: citizenStream,
|
stream: citizenStream,
|
||||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
|
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
|
||||||
if (snapshot.hasError) {
|
if (snapshot.hasError) {
|
||||||
return Text('Error: ${snapshot.error}');
|
return Text('Error: ${snapshot.error}');
|
||||||
}
|
}
|
||||||
@@ -226,13 +409,13 @@ class _CitizensForum extends State<IssueList> {
|
|||||||
case ConnectionState.waiting:
|
case ConnectionState.waiting:
|
||||||
return const Text('Loading...');
|
return const Text('Loading...');
|
||||||
default:
|
default:
|
||||||
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
|
final List<QueryDocumentSnapshot<Map<String, dynamic>>> documents = snapshot.data!.docs;
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
itemCount: documents.length,
|
itemCount: documents.length,
|
||||||
itemBuilder: (BuildContext context, int index) {
|
itemBuilder: (BuildContext context, int index) {
|
||||||
final Map<String, dynamic> data = documents[index].data()! as Map<String, dynamic>;
|
final Map<String, dynamic> data = documents[index].data();
|
||||||
final DateTime createdDate = data['createdDate'].toDate();
|
final DateTime createdDate = data['createdDate'].toDate();
|
||||||
final formattedDate = DateFormat('dd.MM.yyyy').format(createdDate);
|
final formattedDate = DateFormat.yMd().format(createdDate);
|
||||||
return UserDefinedItem(
|
return UserDefinedItem(
|
||||||
title: data['title'] ?? '',
|
title: data['title'] ?? '',
|
||||||
datum: formattedDate,
|
datum: formattedDate,
|
||||||
@@ -245,4 +428,4 @@ class _CitizensForum extends State<IssueList> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|||||||
+30
-35
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'UserDefinedList.dart';
|
import 'UserDefinedList.dart';
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class MyHomePage extends StatefulWidget {
|
||||||
const MyHomePage({super.key, required this.title});
|
const MyHomePage({Key? key, required this.title});
|
||||||
|
|
||||||
final String title;
|
final String title;
|
||||||
|
|
||||||
@@ -14,89 +14,84 @@ class MyHomePage extends StatefulWidget {
|
|||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
int _selectedIndex = 0;
|
int _selectedIndex = 0;
|
||||||
bool login = false;
|
bool login = false;
|
||||||
bool isIssueItemEnabled = true;
|
|
||||||
|
|
||||||
static const TextStyle optionStyle =
|
static const TextStyle optionStyle =
|
||||||
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
|
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
|
||||||
|
|
||||||
void _onItemTapped(int index) {
|
void _onItemTapped(int index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_selectedIndex = index;
|
_selectedIndex = index;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLogin(bool value) {
|
void setLogin(bool value) {
|
||||||
setState(() {
|
setState(() {
|
||||||
login = value;
|
login = value;
|
||||||
isIssueItemEnabled = value;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final List<Widget> widgetOptions = <Widget>[
|
final List<Widget> widgetOptions = <Widget>[
|
||||||
|
const CitizensForum(),
|
||||||
const NewsList(),
|
const NewsList(),
|
||||||
login ? const IssueList(): const Text(
|
const IssueList(),
|
||||||
'Mängel (nur für angemeldete Benutzer verfügbar)',
|
Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
|
||||||
style: optionStyle,
|
ElevatedButton(
|
||||||
textAlign: TextAlign.center,
|
child: Text(login ? 'Logout' : 'Login'),
|
||||||
),
|
onPressed: () {
|
||||||
Column(
|
setLogin(!login);
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
},
|
||||||
children: <Widget>[
|
),
|
||||||
ElevatedButton(
|
const Text(
|
||||||
child: Text(login ? 'Logout' : 'Login'),
|
'Index 2: Profil',
|
||||||
onPressed: () {
|
style: optionStyle,
|
||||||
setLogin(!login);
|
),
|
||||||
},
|
])
|
||||||
),
|
|
||||||
const Text(
|
|
||||||
'Index 2: Profil',
|
|
||||||
style: optionStyle,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Oggauer Gemeinde Trommler'),
|
title: const Text('Oggauer Gemeinde Trommler'),
|
||||||
|
|
||||||
|
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Center(
|
||||||
child: widgetOptions.elementAt(_selectedIndex),
|
child: widgetOptions.elementAt(_selectedIndex),
|
||||||
|
|
||||||
),
|
),
|
||||||
|
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(builder: (context) => AddScreen(initialIndex: _selectedIndex,)),
|
MaterialPageRoute(
|
||||||
|
builder: (context) => AddScreen(initialIndex: _selectedIndex)),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
tooltip: "Add",
|
tooltip: "Add",
|
||||||
child: Icon(Icons.add),
|
child: Icon(Icons.add),
|
||||||
),
|
),
|
||||||
|
|
||||||
bottomNavigationBar: BottomNavigationBar(
|
bottomNavigationBar: BottomNavigationBar(
|
||||||
items: const <BottomNavigationBarItem>
|
items: const <BottomNavigationBarItem>[
|
||||||
[
|
BottomNavigationBarItem(
|
||||||
|
icon: Icon(Icons.person),
|
||||||
|
label: 'BürgerForum',
|
||||||
|
),
|
||||||
BottomNavigationBarItem(
|
BottomNavigationBarItem(
|
||||||
icon: Icon(Icons.newspaper),
|
icon: Icon(Icons.newspaper),
|
||||||
label: 'News',
|
label: 'Neuigkeiten',
|
||||||
),
|
),
|
||||||
BottomNavigationBarItem(
|
BottomNavigationBarItem(
|
||||||
icon: Icon(Icons.report),
|
icon: Icon(Icons.report),
|
||||||
label: 'Mängel',
|
label: 'Mängel',
|
||||||
),
|
),
|
||||||
BottomNavigationBarItem(
|
BottomNavigationBarItem(
|
||||||
icon: Icon(Icons.person),
|
icon: Icon(Icons.account_circle),
|
||||||
label: 'Profil',
|
label: 'Profil',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
currentIndex: _selectedIndex,
|
currentIndex: _selectedIndex,
|
||||||
selectedItemColor: Colors.blue,
|
selectedItemColor: Colors.blue,
|
||||||
|
unselectedItemColor: Colors.black26,
|
||||||
onTap: _onItemTapped,
|
onTap: _onItemTapped,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
//TEST, TEST
|
//TEST, TEST
|
||||||
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'home.dart';
|
import 'home.dart';
|
||||||
|
|||||||
Reference in New Issue
Block a user