Last active
August 4, 2023 12:35
-
-
Save faizan1947/5b04166ae12e3b4b94fff95b83f7c8ea to your computer and use it in GitHub Desktop.
realtimeDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:firebase_database/firebase_database.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:link_preview_generator/link_preview_generator.dart'; | |
// { | |
// "groups": { | |
// "group1": { | |
// "links": { | |
// "link1": { | |
// "title": "Link 1 Title", | |
// "url": "https://example.com/link1" | |
// }, | |
// "link2": { | |
// "title": "Link 2 Title", | |
// "url": "https://example.com/link2" | |
// } | |
// } | |
// }, | |
// "group2": { | |
// "links": { | |
// "link3": { | |
// "title": "Link 3 Title", | |
// "url": "https://example.com/link3" | |
// } | |
// } | |
// } | |
// } | |
// } | |
class RealtimeDbExample extends StatefulWidget { | |
const RealtimeDbExample({super.key}); | |
@override | |
RealtimeDbExampleState createState() => RealtimeDbExampleState(); | |
} | |
class RealtimeDbExampleState extends State<RealtimeDbExample> { | |
final DatabaseReference _databaseReference = | |
FirebaseDatabase.instance.reference(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Group Links'), | |
), | |
body: FutureBuilder<DatabaseEvent>( | |
future: _databaseReference.child('groups').once(), | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return const Center(child: CircularProgressIndicator()); | |
} else if (snapshot.hasError) { | |
return Center(child: Text('Error: ${snapshot.error}')); | |
} else if (!snapshot.hasData || | |
snapshot.data?.snapshot.value == null) { | |
return const Center(child: Text('No data')); | |
} else { | |
final data = snapshot.data?.snapshot.value as Map<dynamic, dynamic>; | |
final groupNames = data.keys.toList(); | |
return ListView.builder( | |
itemCount: groupNames.length, | |
itemBuilder: (context, index) { | |
final groupName = groupNames[index]; | |
final groupData = data[groupName] as Map<dynamic, dynamic>; | |
final links = groupData['links'] as Map<dynamic, dynamic>; | |
return ListTile( | |
title: Text(groupName), | |
subtitle: Text('Links: ${links.length}'), | |
onTap: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => GroupLinksScreen( | |
groupName: groupName, links: links, | |
databaseReference: _databaseReference, // Pass it here | |
), | |
), | |
); | |
}, | |
); | |
}, | |
); | |
} | |
}, | |
), | |
); | |
} | |
} | |
class GroupLinksScreen extends StatelessWidget { | |
final String groupName; | |
final Map<dynamic, dynamic> links; | |
final DatabaseReference databaseReference; // Add this line | |
const GroupLinksScreen( | |
{required this.groupName, | |
required this.links, | |
required this.databaseReference, | |
Key? key}) | |
: super(key: key); | |
void addNewLink(String id, String url) { | |
databaseReference | |
.child('groups') | |
.child(groupName) | |
.child('links') | |
.push() | |
.set({ | |
'id': id, | |
'url': url, | |
}); | |
} | |
void _showAddLinkDialog(BuildContext context) { | |
String newId = ''; | |
String newUrl = ''; | |
showDialog( | |
context: context, | |
builder: (context) { | |
return AlertDialog( | |
title: const Text('Add New Link'), | |
content: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
TextField( | |
onChanged: (value) { | |
newId = value; | |
}, | |
decoration: const InputDecoration( | |
hintText: 'Enter ID', | |
), | |
), | |
TextField( | |
onChanged: (value) { | |
newUrl = value; | |
}, | |
decoration: const InputDecoration( | |
hintText: 'Enter URL', | |
), | |
), | |
], | |
), | |
actions: [ | |
TextButton( | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
child: const Text('Cancel'), | |
), | |
ElevatedButton( | |
onPressed: () { | |
// Call the function to add the new link | |
addNewLink(newId, newUrl); | |
Navigator.of(context).pop(); | |
}, | |
child: const Text('Add'), | |
), | |
], | |
); | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(groupName), | |
), | |
body: ListView.builder( | |
itemCount: links.length, | |
itemBuilder: (context, index) { | |
final linkKey = links.keys.toList()[index]; | |
final linkData = links[linkKey] as Map<dynamic, dynamic>; | |
final uniqueNumber = linkData['id'] as String; | |
final url = linkData['url'] as String; | |
return ListTile( | |
title: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text('ID: $uniqueNumber'), | |
LinkPreviewGenerator( | |
bodyMaxLines: 3, | |
link: url, | |
placeholderWidget: | |
const Center(child: CircularProgressIndicator()), | |
linkPreviewStyle: LinkPreviewStyle.small, | |
showBody: false, | |
showGraphic: true, | |
showDomain: false, | |
), | |
], | |
), | |
trailing: IconButton( | |
icon: const Icon(Icons.copy), | |
onPressed: () { | |
_copyToClipboard(context, url); | |
}, | |
), | |
); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
_showAddLinkDialog(context); | |
}, | |
), | |
); | |
} | |
void _copyToClipboard(BuildContext context, String text) { | |
Clipboard.setData(ClipboardData(text: text)); | |
const snackBar = SnackBar(content: Text('Link copied to clipboard')); | |
ScaffoldMessenger.of(context).showSnackBar(snackBar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment