diff --git a/lib/StockPage.dart b/lib/StockPage.dart index 1ed3b62..8fafa90 100644 --- a/lib/StockPage.dart +++ b/lib/StockPage.dart @@ -1,6 +1,8 @@ +import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'firebase_auth.dart'; import 'stock_list.dart'; import 'package:provider/provider.dart'; import 'stock_ticker.dart'; @@ -57,6 +59,9 @@ class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { + var balance = 0.0; + final DatabaseReference dbRef = FirebaseDatabase.instance.reference().child("users/$currUID"); + return Scaffold( body: Stack(children: [ Container( @@ -67,6 +72,24 @@ class HomePage extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ + FutureBuilder( + future: dbRef.once(), + builder: (context, AsyncSnapshot snapshot) { + if (snapshot.hasData) { + Map values = snapshot.data.value; + return Card( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text("Name: " + values["name"]), + Text("Balance: \$" + values["balance"]), + ], + ), + ); + } + return CircularProgressIndicator(); + } + ), Text("Stocks", style: TextStyle( color: Colors.white, diff --git a/lib/buySell.dart b/lib/buySell.dart index 562f042..d58440c 100644 --- a/lib/buySell.dart +++ b/lib/buySell.dart @@ -1,7 +1,9 @@ +import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'NavPage.dart'; +import 'firebase_auth.dart'; void main() => runApp(BuySell()); final amountController = TextEditingController(); @@ -33,12 +35,33 @@ void initState() { class _BuySell extends State { @override Widget build(BuildContext context) { + var balance = 0.0; + final DatabaseReference dbRef = FirebaseDatabase.instance.reference().child("users/$currUID"); + return Scaffold( appBar: AppBar( title: Text('Number Field increment decrement'), ), body: Column(children: [ Column(children: [ + FutureBuilder( + future: dbRef.once(), + builder: (context, AsyncSnapshot snapshot) { + if (snapshot.hasData) { + Map values = snapshot.data.value; + return Card( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text("Name: " + values["name"]), + Text("Balance: \$" + values["balance"]), + ], + ), + ); + } + return CircularProgressIndicator(); + } + ), SizedBox(height: 50), TextFormField( controller: amountController, diff --git a/lib/firebase_auth.dart b/lib/firebase_auth.dart index 0d570e5..506213e 100644 --- a/lib/firebase_auth.dart +++ b/lib/firebase_auth.dart @@ -74,135 +74,49 @@ void signOutGoogle() async { print("User Sign Out"); } -final ref = firebaseDB.reference().child("users"); - Future signUpWithEmail(String formName, String formEmail, String formPassword, BuildContext context) async { - try { - _auth.createUserWithEmailAndPassword( - email: formEmail, password: formPassword); - } catch (e) { - // Display alert if signup fails for some reason. - showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: Text("Error"), - content: SingleChildScrollView( - child: ListBody( - children: [ - Text(e.code), - Text(e.message), - ], - ), - ), - actions: [ - FlatButton( - child: Text('Ok'), - onPressed: () { - Navigator.of(context).pop(); - }, - ), - ], - ); - }); - } - final FirebaseUser currentUser = await _auth.currentUser(); - UserUpdateInfo userUpdateInfo = UserUpdateInfo(); - userUpdateInfo.displayName = formName; - currentUser.reload(); - print(currentUser.displayName); + final AuthResult authResult = await _auth.createUserWithEmailAndPassword(email: formEmail, password: formPassword); + final FirebaseUser user = authResult.user; - //name = formName; - name = currentUser.displayName; - email = currentUser.email; - currUID = currentUser.uid; + name = formName; + //name = currentUser.displayName; + //UserUpdateInfo userUpdateInfo = UserUpdateInfo(); + //userUpdateInfo.displayName = formName; + email = user.email; + currUID = user.uid; imageUrl = ""; + assert(await user.getIdToken() != null); + // create firebase db reference to access entries final ref = firebaseDB.reference().child("users"); - // check if user exists; if not create new entry in db - ref.orderByChild("uid").equalTo(currentUser.uid); + ref.orderByChild("uid").equalTo(user.uid); DataSnapshot snapshot = - await ref.orderByChild("uid").equalTo(currentUser.uid).once(); + await ref.orderByChild("uid").equalTo(user.uid).once(); if (snapshot.value == null) { print("adding new user"); - ref.push().set({ + ref.child(currUID).set({ // add to database "name": name, "uid": currUID, "email": email, - "photo": imageUrl, + "photo": imageUrl }); } } -Future signUpWithEmail2(String formName, String formEmail, - String formPassword, BuildContext context) async { - print(formName); - print(formEmail); - print(formPassword); - print('123'); - - int succeed = 0; - FirebaseUser currentUser; - - _auth - .createUserWithEmailAndPassword(email: formEmail, password: formPassword) - .then((credential) { - currentUser = credential.user; - UserUpdateInfo userUpdateInfo = UserUpdateInfo(); - userUpdateInfo.displayName = formName; - - currentUser.reload(); - //print(currentUser.displayName); - - name = formName; - //name = currentUser.displayName; - email = currentUser.email; - currUID = currentUser.uid; - imageUrl = ""; - - succeed = 1; - }).catchError((error) { - print(error); - }); +Future signInWithEmail(String formEmail, String formPassword) async { + final AuthResult authResult = await _auth.signInWithEmailAndPassword(email: formEmail, password: formPassword); + final FirebaseUser user = authResult.user; - // Didn't put this in then() because await keyword doesn't work in then(). - if (succeed == 1) { - // create firebase db reference to access entries - - // check if user exists; if not create new entry in db - ref.orderByChild("email").equalTo(currentUser.uid); - DataSnapshot snapshot = - await ref.orderByChild("email").equalTo(currentUser.uid).once(); - if (snapshot.value == null) { - print("adding new user"); - ref.push().set({ - // add to database - "name": name, - "uid": currUID, - "email": email, - "photo": imageUrl - }); - } - } -} + assert(await user.getIdToken() != null); + + email = user.email; + currUID = user.uid; -Future signInWithEmail(String formEmail, String formPassword) async { - _auth - .signInWithEmailAndPassword(email: formEmail, password: formPassword) - .then((credential) { - final FirebaseUser currentUser = credential.user; - email = currentUser.email; - currUID = currentUser.uid; - return; - }).catchError((error) { - print(error); - return; - }); return; } @@ -259,6 +173,7 @@ TextEditingController nameChange = TextEditingController(); TextEditingController bioChange = TextEditingController(); Future update() async { + final ref = firebaseDB.reference().child("users"); final FirebaseUser currentUser = await _auth.currentUser(); ref.orderByChild("uid").equalTo(currentUser.uid); @@ -272,6 +187,7 @@ Future update() async { } Future updateProfile() async { + final ref = firebaseDB.reference().child("users"); final FirebaseUser currentUser = await _auth.currentUser(); ref.orderByChild("uid").equalTo(currentUser.uid); DataSnapshot snapshot =