Home > Back-end >  snackbars not display flutter
snackbars not display flutter

Time:04-13

this is my forget password screen. All the validations and API Integrations are successfully working but the snackbars I have implemented are not working. when I click sign in button it should display as logged in successfully. but doesn't display. how to resolve this issue. ... .......................................................

import 'package:dio/dio.dart';
import 'package:email_validator/email_validator.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:lala_live/constants/colors.dart';
import 'package:lala_live/screens/singup.dart';
import 'package:lala_live/widgets/rectnaglebutton.dart';

import '../constants/base_api.dart';

class ForgetPassword extends StatefulWidget {
  const ForgetPassword({Key? key}) : super(key: key);

  @override
  _ForgetPasswordState createState() => _ForgetPasswordState();
}

class _ForgetPasswordState extends State<ForgetPassword> {

  final Shader linearGradient = LinearGradient(
    colors: <Color>[
      Color(0xffDA0B55),
      Color(0xff7738B5),
    ],
  ).createShader(Rect.fromLTWH(0.0, 0.0, 200.0, 70.0));

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    double widgetheight = height * 0.075;
    return Scaffold(
        backgroundColor: backgroundWhite,
        body: GestureDetector(
            onTap: () {
              FocusScope.of(context).unfocus();
              FocusScope.of(context).requestFocus(new FocusNode());
            },
            child: Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("asset/images/back.png"),
                  fit: BoxFit.fill,
                  alignment: Alignment.topCenter,
                ),
              ),
              child: SingleChildScrollView(
                child: Column(children: [
                  SizedBox(
                    height: height * 0.2,
                  ),
                  Container(
                    width: width,
                    height: height / 0.6,
                    decoration: BoxDecoration(
                      color: backgroundWhite,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20),
                        topRight: Radius.circular(20),
                      ),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: height * 0.05,
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: width * 0.05),
                          child: Text(
                            'Forget Password',
                            style: new TextStyle(
                                fontSize: width * 0.1,
                                fontWeight: FontWeight.bold,
                                foreground: Paint()..shader = linearGradient),
                          ),
                        ),
                        SizedBox(
                          height: height * 0.03,
                        ),
                        const Center(
                          child: ForgetPasswordForm(),
                        ),
                        SizedBox(
                          height: height * 0.02,
                        ),


                        Center(
                          child: RichText(
                              text: TextSpan(children: [
                                TextSpan(
                                    text: "Don’t have an account?",
                                    style: TextStyle(
                                      fontSize: width * 0.04,
                                      color: textBlack,
                                    )),

                                TextSpan(
                                    recognizer: TapGestureRecognizer()
                                      ..onTap = () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) => SignUp()),
                                        );
                                      },
                                    text: "Sign Up",
                                    style: TextStyle(
                                        fontSize: width * 0.04,
                                        color: textpink,
                                        fontWeight: FontWeight.bold)),
                              ])),
                        ),
                        Center(
                          child: TextButton(
                            onPressed: () {},
                            child: Text(
                              'Privacy Policy', //title
                              textAlign: TextAlign.start,
                              style: TextStyle(
                                  color: textGrey,
                                  fontSize: width * 0.035), //aligment
                            ),
                          ),
                        ),
                        SizedBox(
                          height: 50,
                        )
                      ],
                    ),
                  ),
                ]),
              ),
            )));
  }
}

class ForgetPasswordForm extends StatefulWidget {
  const ForgetPasswordForm({Key? key}) : super(key: key);

  @override
  _ForgetPasswordFormState createState() => _ForgetPasswordFormState();
}
Map<String, String> loginUserData = {
  'email': '',
};

class _ForgetPasswordFormState extends State<ForgetPasswordForm> {


  TextEditingController emailEditingController = new TextEditingController();

  final _formKey = GlobalKey<FormState>();
  String email = "";
  bool isLoading = false;
  bool _isObscure = true;
  bool checkedValue = true;
  String fcmToken = '';
  bool pointerIgnore = false;

  Future Resetpassword() async {
    setState(() {
      isLoading = true;
    });
    try {
      var response = await Dio().post(BASE_API  'user/forgotpassword', data: {
        "email": email,
      });

      if (response.data["data"] ==
          "Please check your email to reset password.") {
        setState(() {
          isLoading = false;
        });
        Get.snackbar("success", "Email Sent Successfully!",

          backgroundColor: Colors.purple,
          borderWidth: 1,
          borderColor: Colors.purple,
          colorText: textWhite,);

      } else {
        setState(() {
          isLoading = false;
        });
        Get.snackbar("Error", "No User Found",
            backgroundColor: Colors.purple,
            borderWidth: 1,
            borderColor: Colors.purple,
            colorText: textWhite,
            icon: Icon(
              Icons.error_outline_outlined,
              color: Colors.red,
              size: 30,
            ));
      }
      print("res: $response");
    } catch (e) {
      setState(() {
        isLoading = false;
      });
      Get.snackbar("Error", "Something went wrong.Please contact admin",
          backgroundColor: Colors.purple,
          borderWidth: 1,
          borderColor: Colors.purple,
          colorText: textWhite,
          icon: Icon(
            Icons.error_outline_outlined,
            color: Colors.red,
            size: 30,
          ));
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Form(
        key: _formKey,
        autovalidateMode: AutovalidateMode.disabled,
        child: Container(
          margin: const EdgeInsets.only(left: 20, right: 20),
          child: Column(
            children: [
              const SizedBox(
                height: 20,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text("Enter Your Email Here",
                      style: TextStyle(
                        fontSize: width * 0.05,
                        color: textBlack,
                        fontWeight: FontWeight.bold,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                controller: emailEditingController,
                style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration(
                    fillColor: Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(5),
                        borderSide:
                        BorderSide(width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(5),
                        borderSide:
                        BorderSide(width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(5.0),
                      borderSide: const BorderSide(color: errorRed),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(5.0),
                      borderSide: const BorderSide(color: errorRed),
                    ),
                    isDense: true,
                    border: InputBorder.none),
                validator: (email) {
                  if (EmailValidator.validate(email!)) {
                    return null;
                  } else {
                    return 'Enter a valid email address';
                  }
                },
                onChanged: (String? text) {
                  email = text!;
                  print(email);
                },
              ),
              SizedBox(
                height: height * 0.05,
              ),

              Center(
                  child: ButtonRectangle(
                      text: 'Reset Password',
                      onpress: () async{
                        if (_formKey.currentState!.validate()) {
                          await Resetpassword();
                        }
                      }
                        ),
    ),
            ],
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

You need to change Material App to GetMaterialApp

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  runApp(const GetMaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter SnackBar'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            ElevatedButton(
              child: const Text('Show SnackBar'),
              onPressed: () {
                Get.snackbar('success', 'Email Sent Successfully!',
                    snackPosition: SnackPosition.BOTTOM);
              },
            ),
          ],
        ),
      ),
    );
  }
}
  • Related