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
|
||||
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);
|
||||
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,
|
||||
"description": description,
|
||||
"imageUrl": imageUrl,
|
||||
@@ -60,10 +73,10 @@ class _AddScreenState extends State<AddScreen> {
|
||||
}
|
||||
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);
|
||||
String imageUrl = await (await uploadTask).ref.getDownloadURL();
|
||||
await FirebaseFirestore.instance.collection("News").add({
|
||||
await FirebaseFirestore.instance.collection("Issues").add({
|
||||
"title": title,
|
||||
"description": description,
|
||||
"imageUrl": imageUrl,
|
||||
|
||||
+196
-13
@@ -93,10 +93,6 @@ class UserDefinedItem extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class NewsList extends StatefulWidget {
|
||||
const NewsList({Key? key}) : super(key: key);
|
||||
|
||||
@@ -104,6 +100,193 @@ class NewsList extends StatefulWidget {
|
||||
_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> {
|
||||
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||
late Stream<QuerySnapshot> newsStream;
|
||||
@@ -201,12 +384,12 @@ class CitizensForum extends StatefulWidget {
|
||||
const CitizensForum({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CitizensForum createState() => _CitizensForum();
|
||||
_CitizensForumState createState() => _CitizensForumState();
|
||||
}
|
||||
|
||||
class _CitizensForum extends State<IssueList> {
|
||||
class _CitizensForumState extends State<CitizensForum> {
|
||||
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
||||
late Stream<QuerySnapshot> citizenStream;
|
||||
late Stream<QuerySnapshot<Map<String, dynamic>>> citizenStream;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -216,9 +399,9 @@ class _CitizensForum extends State<IssueList> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return StreamBuilder<QuerySnapshot>(
|
||||
return StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
|
||||
stream: citizenStream,
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Text('Error: ${snapshot.error}');
|
||||
}
|
||||
@@ -226,13 +409,13 @@ class _CitizensForum extends State<IssueList> {
|
||||
case ConnectionState.waiting:
|
||||
return const Text('Loading...');
|
||||
default:
|
||||
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
|
||||
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()! as Map<String, dynamic>;
|
||||
final Map<String, dynamic> data = documents[index].data();
|
||||
final DateTime createdDate = data['createdDate'].toDate();
|
||||
final formattedDate = DateFormat('dd.MM.yyyy').format(createdDate);
|
||||
final formattedDate = DateFormat.yMd().format(createdDate);
|
||||
return UserDefinedItem(
|
||||
title: data['title'] ?? '',
|
||||
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';
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
const MyHomePage({Key? key, required this.title});
|
||||
|
||||
final String title;
|
||||
|
||||
@@ -14,89 +14,84 @@ class MyHomePage extends StatefulWidget {
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
int _selectedIndex = 0;
|
||||
bool login = false;
|
||||
bool isIssueItemEnabled = true;
|
||||
|
||||
static const TextStyle optionStyle =
|
||||
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
|
||||
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
|
||||
|
||||
void _onItemTapped(int index) {
|
||||
setState(() {
|
||||
_selectedIndex = index;
|
||||
});
|
||||
}
|
||||
|
||||
void setLogin(bool value) {
|
||||
setState(() {
|
||||
login = value;
|
||||
isIssueItemEnabled = value;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final List<Widget> widgetOptions = <Widget>[
|
||||
const CitizensForum(),
|
||||
const NewsList(),
|
||||
login ? const IssueList(): const Text(
|
||||
'Mängel (nur für angemeldete Benutzer verfügbar)',
|
||||
style: optionStyle,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
ElevatedButton(
|
||||
child: Text(login ? 'Logout' : 'Login'),
|
||||
onPressed: () {
|
||||
setLogin(!login);
|
||||
},
|
||||
),
|
||||
const Text(
|
||||
'Index 2: Profil',
|
||||
style: optionStyle,
|
||||
),
|
||||
],
|
||||
),
|
||||
const IssueList(),
|
||||
Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
|
||||
ElevatedButton(
|
||||
child: Text(login ? 'Logout' : 'Login'),
|
||||
onPressed: () {
|
||||
setLogin(!login);
|
||||
},
|
||||
),
|
||||
const Text(
|
||||
'Index 2: Profil',
|
||||
style: optionStyle,
|
||||
),
|
||||
])
|
||||
];
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Oggauer Gemeinde Trommler'),
|
||||
|
||||
|
||||
),
|
||||
body: Center(
|
||||
child: widgetOptions.elementAt(_selectedIndex),
|
||||
|
||||
),
|
||||
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => AddScreen(initialIndex: _selectedIndex,)),
|
||||
MaterialPageRoute(
|
||||
builder: (context) => AddScreen(initialIndex: _selectedIndex)),
|
||||
);
|
||||
},
|
||||
tooltip: "Add",
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: const <BottomNavigationBarItem>
|
||||
[
|
||||
items: const <BottomNavigationBarItem>[
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.person),
|
||||
label: 'BürgerForum',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.newspaper),
|
||||
label: 'News',
|
||||
label: 'Neuigkeiten',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.report),
|
||||
label: 'Mängel',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.person),
|
||||
icon: Icon(Icons.account_circle),
|
||||
label: 'Profil',
|
||||
),
|
||||
],
|
||||
currentIndex: _selectedIndex,
|
||||
selectedItemColor: Colors.blue,
|
||||
unselectedItemColor: Colors.black26,
|
||||
onTap: _onItemTapped,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//TEST, TEST
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'home.dart';
|
||||
|
||||
Reference in New Issue
Block a user