'Flutter : starting the app with welcome page
I'm new to flutter and in my new project I've created a welcome screen that have 2 buttons either login or sign up , I'm using a Wrapper for the authentication and a toggle to navigate between login and sign up ... every thing works fine , but once the app lunches it shows the login screen not the welcome screen !
here is my wrapper class
class wrapper extends StatelessWidget {
const wrapper({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final userModel = Provider.of<UserModel?>(context);
// either home or login page
if(userModel == null){
return const Authenticate();
}else{
return const Home();
}
}
}
and here is my main
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<UserModel?>.value(
initialData: null,
value: Authentication().onAuthStateChanged,
builder: (context, snapshot) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Auth',
theme: ThemeData(
primaryColor: kPrimaryColor,
scaffoldBackgroundColor: Colors.white,
textTheme: GoogleFonts.nunitoTextTheme(),
),
home: wrapper(),
);
}
);
}
}
the authenticate class :
class Authenticate extends StatefulWidget {
const Authenticate({Key? key}) : super(key: key);
@override
_AuthenticateState createState() => _AuthenticateState();
}
class _AuthenticateState extends State<Authenticate> {
bool showSignIn = true ;
void toggleView() {
setState(() {
showSignIn = !showSignIn;
});
}
@override
Widget build(BuildContext context) {
if(showSignIn){
return LoginScreen(toggleView : toggleView);
}else{
return SignUpScreen(toggleView : toggleView);
}
}
}
welcome screen :
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Body(),
);
}
}
Body :
class Body extends StatelessWidget {
const Body({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
height: size.height,
width: double.infinity,
color: Color(0xFFA9D7CC),
child:Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(width: size.width,height: 70),
SvgPicture.asset(
"assets/images/recycle.svg" ,
width:441.4,
height: 294 ,
),
const Padding(
padding: EdgeInsets.fromLTRB(20, 40, 7, 0),
child:
Text("lets make saving the planet easy !",
style: TextStyle(
fontSize: 48 ,
fontWeight: FontWeight.bold,
color: Colors.white
),
),
),
const Padding(
padding: EdgeInsets.fromLTRB(27, 10, 110, 0),
child:
Text("start recycling and earn some money ",
style: TextStyle(
fontSize: 20 ,
fontWeight: FontWeight.normal,
color: Colors.white
),
),
),
SizedBox(width: size.width,height: 70),
// login button
SizedBox(
width: 297,
height: 71,
child: ElevatedButton(
style:ElevatedButton.styleFrom(
primary: kPrimaryPopColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40)
)
) ,
onPressed: (){},
child: const Text("login",
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold
),)),
),
//sign up button
SizedBox(
height: 70,
child: TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
),
onPressed: () {},
child: const Text('sign up',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),),
),
)
],
),
);
}
}
Solution 1:[1]
Your code in not full. Try to debug https://docs.flutter.dev/development/tools/devtools/debugger It seems to me, that here is true
if(userModel == null){ return const Authenticate();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Evgen |
