Last active
October 3, 2024 15:17
-
-
Save AashishNandakumar/3033e1e1e6dcb7c9b820781ae8dcee38 to your computer and use it in GitHub Desktop.
ChallengeHub Flutter App
This file contains hidden or 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_core/firebase_core.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
void main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
await Firebase.initializeApp(); | |
runApp(ChallengeHubApp()); | |
} | |
class ChallengeHubApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'ChallengeHub', | |
theme: ThemeData.light(), | |
darkTheme: ThemeData.dark(), | |
themeMode: ThemeMode.system, | |
home: AuthWrapper(), | |
); | |
} | |
} | |
class AuthWrapper extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return StreamBuilder<User?>( | |
stream: FirebaseAuth.instance.authStateChanges(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return HomePage(); | |
} | |
return LoginPage(); | |
}, | |
); | |
} | |
} | |
class LoginPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Login')), | |
body: Center( | |
child: ElevatedButton( | |
child: Text('Login with Google'), | |
onPressed: () async { | |
// Implement Google Sign-In | |
}, | |
), | |
), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
int _currentIndex = 0; | |
final List<Widget> _pages = [ | |
ChallengesPage(), | |
LeaderboardPage(), | |
ProfilePage(), | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('ChallengeHub')), | |
body: _pages[_currentIndex], | |
bottomNavigationBar: BottomNavigationBar( | |
currentIndex: _currentIndex, | |
onTap: (index) { | |
setState(() { | |
_currentIndex = index; | |
}); | |
}, | |
items: [ | |
BottomNavigationBarItem(icon: Icon(Icons.code), label: 'Challenges'), | |
BottomNavigationBarItem(icon: Icon(Icons.leaderboard), label: 'Leaderboard'), | |
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'), | |
], | |
), | |
); | |
} | |
} | |
class ChallengesPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return FutureBuilder<List<Challenge>>( | |
future: fetchChallenges(), | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return Center(child: CircularProgressIndicator()); | |
} else if (snapshot.hasError) { | |
return Center(child: Text('Error: ${snapshot.error}')); | |
} else { | |
return ListView.builder( | |
itemCount: snapshot.data!.length, | |
itemBuilder: (context, index) { | |
return ChallengeCard(challenge: snapshot.data![index]); | |
}, | |
); | |
} | |
}, | |
); | |
} | |
Future<List<Challenge>> fetchChallenges() async { | |
// Implement API call to fetch challenges | |
// For now, return dummy data | |
return [ | |
Challenge(id: '1', title: 'Algorithmic Puzzle', difficulty: 'Medium'), | |
Challenge(id: '2', title: 'UI Challenge', difficulty: 'Hard'), | |
]; | |
} | |
} | |
class Challenge { | |
final String id; | |
final String title; | |
final String difficulty; | |
Challenge({required this.id, required this.title, required this.difficulty}); | |
} | |
class ChallengeCard extends StatelessWidget { | |
final Challenge challenge; | |
ChallengeCard({required this.challenge}); | |
@override | |
Widget build(BuildContext context) { | |
return Card( | |
child: ListTile( | |
title: Text(challenge.title), | |
subtitle: Text('Difficulty: ${challenge.difficulty}'), | |
onTap: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => ChallengeDetailPage(challenge: challenge), | |
), | |
); | |
}, | |
), | |
); | |
} | |
} | |
class ChallengeDetailPage extends StatelessWidget { | |
final Challenge challenge; | |
ChallengeDetailPage({required this.challenge}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text(challenge.title)), | |
body: Padding( | |
padding: EdgeInsets.all(16.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text('Difficulty: ${challenge.difficulty}'), | |
SizedBox(height: 16), | |
Text('Description: Lorem ipsum...'), // Replace with actual description | |
SizedBox(height: 16), | |
ElevatedButton( | |
child: Text('Submit Solution'), | |
onPressed: () { | |
// Implement solution submission | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class LeaderboardPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Center(child: Text('Leaderboard - Coming Soon')); | |
} | |
} | |
class ProfilePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final user = FirebaseAuth.instance.currentUser; | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
CircleAvatar( | |
radius: 50, | |
backgroundImage: NetworkImage(user?.photoURL ?? ''), | |
), | |
SizedBox(height: 16), | |
Text(user?.displayName ?? 'User'), | |
SizedBox(height: 16), | |
ElevatedButton( | |
child: Text('Sign Out'), | |
onPressed: () async { | |
await FirebaseAuth.instance.signOut(); | |
}, | |
), | |
], | |
), | |
); | |
} | |
} |
This file contains hidden or 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
dependencies: | |
flutter: | |
sdk: flutter | |
firebase_core: ^1.10.0 | |
firebase_auth: ^3.3.0 | |
cloud_firestore: ^3.1.0 | |
http: ^0.13.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment