RatelHub / rflutter_alert

RFlutter Alert is super customizable and easy-to-use alert / popup dialog library for Flutter. You may create reusable alert styles or add buttons as much as you want with ease.
https://ratel.com.tr
MIT License
386 stars 117 forks source link

context : context in Alert(); is throwing an error #88

Closed Prophet1702 closed 3 years ago

Prophet1702 commented 3 years ago

I'm new to Flutter . Any help is appreciated. Thanks in advance. The error is : Undefined name 'context'. Try correcting the name to one that is defined, or defining the name. Here's my code :

import 'package:flutter/material.dart';
import 'package:quizzler/quiz_brain.dart';
import 'package:rflutter_alert/rflutter_alert.dart';

void main() => runApp(Quizzler());

class Quizzler extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.grey.shade900,
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.0),
            child: QuizPage(),
          ),
        ),
      ),
    );
  }
}

List<Icon> scoreKeeper = [];

void checkUserAnswer(bool userPickedAnswer) {
  bool correctAnswer = quizBrain.getQuestionAnswer();
//TODO: Step 4 - Use IF/ELSE to check if we've reached the end of the quiz. If true, execute Part A, B, C, D.
  //TODO: Step 4 Part A - show an alert using rFlutter_alert (remember to read the docs for the package!)
  //HINT! Step 4 Part B is in the quiz_brain.dart
  //TODO: Step 4 Part C - reset the questionNumber,
  //TODO: Step 4 Part D - empty out the scoreKeeper.

  //TODO: Step 5 - If we've not reached the end, ELSE do the answer checking steps below

  if (quizBrain.isFinished() == true) {
    Alert(
      context: context,
      type: AlertType.error,
      title: "RFLUTTER ALERT",
      desc: "Flutter is more awesome with RFlutter Alert.",
      buttons: [
        DialogButton(
          child: Text(
            "COOL",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          onPressed: () => Navigator.pop(context),
          width: 120,
        )
      ],
    ).show();
    quizBrain.reset();
    scoreKeeper = [];
  } else {
    if (correctAnswer == userPickedAnswer) {
      scoreKeeper.add(Icon(
        Icons.check,
        color: Colors.green,
      ));
    } else {
      scoreKeeper.add(Icon(
        Icons.close,
        color: Colors.red,
      ));
    }
    quizBrain.nextQuestion();
  }

  //The user picked true.
}
// List<String> questions = [
//   'You can lead a cow down stairs but not up stairs.',
//   'Approximately one quarter of human bones are in the feet.',
//   'A slug\'s blood is green.'
// ];
// List<bool> answers = [
//   false,
//   true,
//   true,
// ];

// List<Question> questionBank = [
//   Question(q: 'You can lead a cow down stairs but not up stairs.', a: false),
//   Question(
//       q: 'Approximately one quarter of human bones are in the feet.', a: true),
//   Question(q: 'A slug\'s blood is green.', a: true),
// ];

QuizBrain quizBrain = QuizBrain();

class QuizPage extends StatefulWidget {
  @override
  _QuizPageState createState() => _QuizPageState();
}

class _QuizPageState extends State<QuizPage> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Expanded(
          flex: 5,
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Center(
              child: Text(
                quizBrain.getQuestionText(),
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 25.0,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(15.0),
            child: FlatButton(
              textColor: Colors.white,
              color: Colors.green,
              child: Text(
                'True',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20.0,
                ),
              ),
              onPressed: () {
                setState(() {
                  checkUserAnswer(true);
                });
              },
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(15.0),
            child: FlatButton(
              color: Colors.red,
              child: Text(
                'False',
                style: TextStyle(
                  fontSize: 20.0,
                  color: Colors.white,
                ),
              ),
              onPressed: () {
                //The user picked false.
                setState(() {
                  checkUserAnswer(false);
                });
              },
            ),
          ),
        ),
        //TODO: Add a Row here as your score keeper
        //TODO: This is how u create a to do
        Expanded(
          child: Row(
            children: scoreKeeper,
          ),
        )
      ],
    );
  }
}

/*
question1: 'You can lead a cow down stairs but not up stairs.', false,
question2: 'Approximately one quarter of human bones are in the feet.', true,
question3: 'A slug\'s blood is green.', true,
*/
ibrahimdevs commented 3 years ago

@Prophet1702 I think this issue is not related our plugin. It is much more flutter and coding question which you should ask it on stackoverflow.

I try to help you anyway. You're calling "checkUserAnswer" function, when you cal it you have to provide current screen's context to display Alert. So you can change your void checkUserAnswer(bool userPickedAnswer) function to void checkUserAnswer(bool userPickedAnswer, BuildContext context) and call it checkUserAnswer(false, context)