简体   繁体   中英

Sign in button not working with firebase authentication

Hi there people i cannot run the code on this and the sign in button doesnt respond to the firebase i have tried all the things but couldnt figure out the problem can you please help me on this one


import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:my_project/hello/Courses.dart';
import 'package:url_launcher/url_launcher.dart';

class MyLogin extends StatefulWidget {
  const MyLogin({super.key});

  @override
  State<MyLogin> createState() => _MyLoginState();
}

class _MyLoginState extends State<MyLogin> {

final emailcontroller = TextEditingController();
  final passwordcontroller = TextEditingController();
   
   Future SignIn() async{
    await FirebaseAuth.instance.signInWithEmailAndPassword(email: emailcontroller.text.trim(), password: 
    passwordcontroller.text.trim());
   }
   @override
  void dispose() {
    emailcontroller.dispose();
    passwordcontroller.dispose();
    // TODO: implement dispose
    super.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(image: AssetImage('assets/hello.jpg'),fit: BoxFit.cover)
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: Stack(
          children: [
            Container(
              padding: EdgeInsets.only(left:35,top: 45),
              child: Text('Welcome Back,\nStudent',
               style: TextStyle(color: Color.fromARGB(255, 255, 255, 255),fontSize: 20),),
               
            ),
            SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.3,
                right: 35,
                left: 35),
                child: Column(children: [
                  TextField(
                    controller: emailcontroller,
                    
                    decoration: InputDecoration(
                      fillColor: Colors.grey.shade100,
                      filled: true,
                      hintText: 'Email',
                      border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))
                    ),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  TextField(
                    controller: passwordcontroller,
                    
                    obscureText: true,
                    decoration: InputDecoration(
                      fillColor: Colors.grey.shade100,
                      filled: true,
                      hintText: 'Password',
                      border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))
                    ),
                  ),
                  SizedBox(height: 40,),
                 ElevatedButton.icon(style: ElevatedButton.styleFrom(
                  minimumSize: Size.fromHeight(50),
                 ), icon: Icon(Icons.lock_open,size: 32,), label: Text(
                  'Sign In', style: TextStyle(fontSize: 24),
                 ), onPressed: SignIn,),

            

                
           RichText(
            text: new TextSpan(
              children: [
                new TextSpan(
                  text: 'Forgot Your Password?, ',
                  style: new TextStyle(color: Color.fromARGB(255, 226, 218, 218)),
                ),
                new TextSpan(
                  text: 'Click Here',
                  style: new TextStyle(color: Colors.blue),
                  recognizer: new TapGestureRecognizer()
                    ..onTap = () { launch('https://www.dandenongclc.org.au/about-us');
                  },
                ),
              ],
            ),
          ),
                ]),
              ),
            )
          ],
        ),
      ),
    );
  }
}
import 'package:flutter/material.dart';
import 'package:my_project/hello/Courses.dart';

import 'package:my_project/hello/timetable.dart';
import 'login.dart';
import 'package:flutter/foundation.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';

Future main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(
    MaterialApp(
    debugShowCheckedModeBanner: false, 
    initialRoute: 'login',
    routes: {
      'login':(context) => const MyLogin()
    },
    home: const Mainpage(),
  ));
}
 
class Mainpage extends StatelessWidget {
  const Mainpage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<User?>(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (context, snapshot) 
        {
          if (snapshot.hasData){
            return Courses();
          }
          
          
          return MyLogin();
        }
      ),);
  }
}

 

I was expecting the sign in button to configure with firebase as i have added the user authentication with email and password on firebase but whenever i click the sign i cannot go through to the page i linked to here are my main.dart and my login.dart file can you help guys

In Your Mylogin file use like this

 ElevatedButton.icon(style: ElevatedButton.styleFrom(
                  minimumSize: Size.fromHeight(50),
                 ), icon: Icon(Icons.lock_open,size: 32,), label: Text(
                  'Sign In', style: TextStyle(fontSize: 24),
                 ),onPressed: ()async {
                   await SignIn();
                  },),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM