Приложение Flutter, использующее поставщика, перестраивает свою страницу информационной панели, по которой перемещается страница spla sh, если пользователь вошел в систему. Но страница приборной панели постоянно перестраивается. Обе страницы используют провайдера для прослушивания: true.
неисправная страница панели инструментов:
Это просто текст в центре страницы, показывающий адрес электронной почты пользователя.
main.dart
import 'package:flutter/material.dart';
import 'package:foodie/screens/about.dart';
import 'package:foodie/screens/dashboard.dart';
import 'package:foodie/screens/history.dart';
import 'package:foodie/screens/login.dart';
import 'package:foodie/screens/notification.dart';
import 'package:foodie/screens/profile.dart';
import 'package:foodie/screens/splash.dart';
import 'package:foodie/screens/wallet.dart';
import 'package:foodie/services/auth.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<AuthService>(
create: (context) => AuthService(),
),
],
child: MaterialApp(
title: 'Foodie',
debugShowCheckedModeBanner: false,
theme: ThemeData.light().copyWith(primaryColor: Colors.teal),
darkTheme: ThemeData.dark(),
routes: {
'/': (_) => SplashScreen(),
LoginScreen.routeName: (_) => LoginScreen(),
SplashScreen.routeName: (_) => SplashScreen(),
DashboardScreen.routeName: (_) => DashboardScreen(),
ProfileScreen.routeName: (_) => ProfileScreen(),
HistoryScreen.routeName: (_) => HistoryScreen(),
WalletScreen.routeName: (_) => WalletScreen(),
AboutScreen.routeName: (_) => AboutScreen(),
NotificationScreen.routeName: (_) => NotificationScreen(),
},
),
);
}
}
spla sh .dart
import 'package:flutter/material.dart';
import 'package:flutter_custom_clippers/flutter_custom_clippers.dart';
import 'package:foodie/components/loader.dart';
import 'package:foodie/screens/dashboard.dart';
import 'package:foodie/screens/login.dart';
import 'package:foodie/services/auth.dart';
import 'package:provider/provider.dart';
class SplashScreen extends StatelessWidget {
static const routeName = '/splash-screen';
@override
Widget build(BuildContext context) {
final authService = Provider.of<AuthService>(context);
authService.trySignIn().then((success) {
if (success == false) {
Navigator.of(context).pushNamed(LoginScreen.routeName);
} else {
Navigator.pushNamed(context, DashboardScreen.routeName);
}
});
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
flex: 2,
child: ClipPath(
clipper: OvalBottomBorderClipper(),
child: Container(
color: Theme.of(context).primaryColor,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Image.asset('assets/images/logo.png'),
Text(
'Foodie',
style: Theme.of(context).textTheme.headline1.apply(
color: Theme.of(context).colorScheme.onPrimary,
fontSizeFactor: 0.75,
),
textAlign: TextAlign.center,
),
],
),
),
),
),
Expanded(
flex: 1,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Loader(),
],
),
),
],
),
);
}
}
auth.dart
import 'package:flutter/foundation.dart';
import 'package:foodie/models/user.dart';
import 'package:google_sign_in/google_sign_in.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
class AuthService extends ChangeNotifier {
User _currentUser = User();
User get currentUser => _currentUser;
bool get isLoggedIn {
return _currentUser.email != null;
}
set currentUser(User user) {
_currentUser = user;
notifyListeners();
}
Future<bool> signInWithGoogle() async {
try {
final GoogleSignInAccount googleSignInAccount =
await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult =
await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser firebaseUser = await _auth.currentUser();
assert(user.uid == firebaseUser.uid);
assert(user.email != null);
assert(user.displayName != null);
assert(user.photoUrl != null);
currentUser = User(
fullName: user.displayName,
email: user.email,
avatarUrl: user.photoUrl,
id: user.uid,
);
} catch (error) {
print(error);
return false;
}
return true;
}
Future<bool> trySignIn() async {
final FirebaseUser user = await _auth.currentUser();
try {
currentUser = User(
fullName: user.displayName,
email: user.email,
avatarUrl: user.photoUrl,
id: user.uid,
);
} catch (e) {
return false;
}
return true;
}
Future<void> signOut() async {
await googleSignIn.signOut();
_auth.signOut();
currentUser = User();
}
}
dashboard.dart
import 'package:foodie/components/drawer.dart';
import 'package:foodie/services/auth.dart';
import 'package:provider/provider.dart';
class DashboardScreen extends StatelessWidget {
static const routeName = '/dashboard-screen';
@override
Widget build(BuildContext context) {
final currentUser =
Provider.of<AuthService>(context, listen: false).currentUser;
return Scaffold(
appBar: AppBar(),
drawer: DrawerMenu(
context: context,
),
body: Center(
child: Text(
currentUser.email,
style: Theme.of(context).textTheme.headline5,
),
),
);
}
}