Ad

Unable To Implement 'if Int Value >= 75' Then Proceed To Next Page Logic

I am working on a certificate generation module where i want to generate certificate only if the input int value of 'percentage' is greater or equal to 75.

I want to implement a function which will able me to only show the certificate page only if the int percentage value is greater than or equal to 75 and execute that function in the submit button.

function which i am implementing in current state:

  void checkpercent(int a) {
    if (percentage >= 75) {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => const CertScreen()));
    }
  }

[In my current code i am able to access the certification page with any numerical value no matter less or more than 75]

but whatever method i am using to implement this logic either i am able to access the certification page with any value or not able to access the page at all.

Since course module is not complete yet which will provide my module the int percentage value i have added textfield which accepts value and converts the string value into integer called percentage for the time being[using "int.parse"].

the code is as follows:[I am having zero warnings,errors in my code while debugging, i am just not able to implement that logic properly.]

main.dart

import 'package:certi_gen_v68/cerstcreen.dart';
import 'package:flutter/material.dart';
import 'cerstcreen.dart';

TextEditingController unameController = TextEditingController();
TextEditingController cnameController = TextEditingController();
TextEditingController auth1Controller = TextEditingController();
TextEditingController auth2Controller = TextEditingController();
TextEditingController percentController = TextEditingController();
int percentage = int.parse(percentController.text);

void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(
          title:
              'Input data [This is temporary screen till modules are integrated]'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void checkpercent(int a) {
    if (percentage >= 75) {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => const CertScreen()));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            TextField(
              controller: unameController,
              decoration: const InputDecoration(
                border: InputBorder.none,
                labelText: 'Enter Full Name',
              ),
            ),
            TextField(
              controller: cnameController,
              decoration: const InputDecoration(
                border: InputBorder.none,
                labelText: 'Enter Course Name',
              ),
            ),
            TextField(
              controller: auth1Controller,
              decoration: const InputDecoration(
                border: InputBorder.none,
                labelText: 'Certification Authority 1',
              ),
            ),
            TextField(
              controller: auth2Controller,
              decoration: const InputDecoration(
                border: InputBorder.none,
                labelText: 'Certification Authority 2',
              ),
            ),
            TextField(
              controller: percentController,
              keyboardType: TextInputType.number,
              decoration: const InputDecoration(
                border: InputBorder.none,
                labelText: 'Percentage',
              ),
            ),
            ElevatedButton(
              child: const Text('Submit'),
              onPressed: () {
                checkpercent(percentage);
              },
            ),
          ],
        ),
      ),
    );
  }
}

certification screen is incomplete just added a few textlabels for now [i am just trying out new syntaxes and styles in certpage]

import 'package:flutter/material.dart';
import 'main.dart' as mainpage;

class CertScreen extends StatelessWidget {
  const CertScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            RichText(
              text: const TextSpan(
                text: 'home',
                style: TextStyle(fontStyle: FontStyle.italic),
                children: <TextSpan>[
                  TextSpan(
                      text: ' bold',
                      style: TextStyle(fontWeight: FontWeight.bold)),
                  TextSpan(text: ' world!'),
                ],
              ),
            ),
            Text(mainpage.unameController.text),
            Text(mainpage.percentage.toString())
          ],
        ),
      ),
    );
  }
}
Ad

Answer

You're not using the parameter a, you use percentage on the if statement.

void checkpercent(int a) {
    if (a >= 75) {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => const CertScreen()));
    }
  }

Also your int percentage = int.parse(percentController.text); at the start of main.dart should be called before the checkpercent function so it receives the latest value.

ElevatedButton(
          child: const Text('Submit'),
          onPressed: () {
            int percentage = int.parse(percentController.text);
            checkpercent(percentage);
          },
        ),
Ad
source: stackoverflow.com
Ad