我已经为我创建了这个新项目,在那里我没有任何类型的错误,但是我无法在Firebase中看到我的用户。我无法在此处打开我的导航器,该导航器应在我登录后自动打开。尽管尽快打开该应用程序,但它显示了Google帐户,这是代码。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'chat app',
theme: ThemeData(
primarySwatch: Colors.pink,
accentColor: Colors.purple,
),
home: MyHomePage(),
);
}
}
这是我登录的主页
// /import 'dart:ffi';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:fluttershare/pages/create_account.dart';
import 'package:google_sign_in/google_sign_in.dart';
import './pages/timeline.dart';
import './pages/upload.dart';
import './pages/activity_feed.dart';
import './pages/search.dart';
import './pages/profile.dart';
final GoogleSignIn googleSignIn = GoogleSignIn();
final usersRef = Firestore.instance.collection("users");
final timestamp = DateTime.now();
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isAuth = false;
int index = 0;
PageController pageController;
@override
void initState() {
super.initState();
pageController = PageController(initialPage: 2);
googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
handleSignIn(account);
});
googleSignIn.signInSilently(suppressErrors: false).then((account) {
handleSignIn(account);
}).catchError((e) {
print(e);
});
}
void dispose() {
pageController.dispose();
super.dispose();
}
handleSignIn(GoogleSignInAccount account) {
if (account != null) {
setState(
() {
isAuth = true;
},
);
} else {
setState(() {
isAuth = false;
});
}
}
Future<void> createUserInFireStore() async {
final GoogleSignInAccount user = googleSignIn.currentUser;
DocumentSnapshot doc = await usersRef.document(user.id).get();
if (!doc.exists) {
String userName;
userName = await Navigator.of(context)
.push(MaterialPageRoute(builder: (conetext) => CreateAccount()));
userRef.document(user.id).setData({
"id": user.id,
"userNAme": userName,
"photoUrl": user.photoUrl,
"email": user.email,
"displayName": user.displayName,
"bio": "",
"timeStamp": timestamp
});
}
}
login() {
googleSignIn.signIn();
}
logout() {
googleSignIn.signOut();
}
onPageChanged(int index) {
setState(() {
this.index = index;
});
}
onTap(int index) {
pageController.animateToPage(
index,
duration: Duration(milliseconds: 250),
curve: Curves.fastOutSlowIn,
);
}
Widget buildAuthScreen() {
return Scaffold(
body: PageView(
children: <Widget>[
RaisedButton(child: Text("logout"), onPressed: logout),
// Timeline(),
ActivityFeed(),
Upload(),
Search(),
Profile(),
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.whatshot), title: Text("timeline")),
BottomNavigationBarItem(
icon: Icon(Icons.notifications_active),
title: Text("activity feed")),
BottomNavigationBarItem(
icon: Icon(
Icons.photo_camera,
),
title: Text("upload")),
BottomNavigationBarItem(
icon: Icon(Icons.search), title: Text("search")),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle), title: Text("profile")),
],
selectedItemColor: Theme.of(context).primaryColor,
selectedIconTheme: IconThemeData(
size: 35,
),
showSelectedLabels: true,
unselectedItemColor: Colors.grey,
backgroundColor: Theme.of(context).accentColor,
onTap: onTap,
currentIndex: index,
type: BottomNavigationBarType.shifting,
),
);
}
Scaffold buildUnAuthScreen() {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.pink,
Colors.purple,
])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Card(
elevation: 8,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.purple,
Colors.pink,
])),
child: Text(
"Whore Market",
style: TextStyle(
fontFamily: "Bebas",
fontSize: 80,
color: Colors.white,
),
textAlign: TextAlign.center,
),
),
),
GestureDetector(
onTap: () => login(),
child: Container(
width: 260,
height: 60,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/google_signin_button.png"),
fit: BoxFit.cover,
),
),
),
)
],
),
),
);
}
@override
Widget build(BuildContext context) {
return isAuth ? buildAuthScreen() : buildUnAuthScreen();
}
}
这是我要导航到的页面
import 'package:flutter/material.dart';
import 'package:fluttershare/widgets/header.dart';
class CreateAccount extends StatefulWidget {
@override
_CreateAccountState createState() => _CreateAccountState();
}
class _CreateAccountState extends State<CreateAccount> {
String userName;
final _formKey =GlobalKey<FormState>();
submit() {
_formKey.currentState.save();
Navigator.pop(context, userName);
}
@override
Widget build(BuildContext parentContext) {
return Scaffold(
appBar: header(context, title: "set up your profile"),
body: ListView(
children: <Widget>[
Container(
height: 50,
width: 350,
child: Form(
key: _formKey,
child: TextFormField(
onSaved: (val)=> userName = val,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "username",
labelStyle: TextStyle(fontSize: 15.0),
hintText: "must have 8 char"),
))),
GestureDetector(
onTap: submit(),
child: Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(7.0)
),
child: Center(
child: Text("Submit",
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
)),
),
),
)
],
),
);
}
}
在“ CreateUserinFireStore”函数中,将“如果”条件内的代码更改为以下代码,
if (!doc.exists) {
await userRef.document(user.id).setData({
"id": user.id,
"userNAme": userName,
"photoUrl": user.photoUrl,
"email": user.email,
"displayName": user.displayName,
"bio": "",
"timeStamp": timestamp
});
Navigator.of(context)
.push(MaterialPageRoute(builder: (conetext) => CreateAccount()));
}