Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to send Data to other screen?

import 'package:flutter/material.dart';
import 'package:flutter_bmi_app/second_screen.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

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

  @override
  State<BmiCalc> createState() => _BmiCalcState();
}

class _BmiCalcState extends State<BmiCalc> {
  Color colorOfLittleBox = Color.fromARGB(255, 27, 28, 48);
  Color colorOfLittleBox2 = Colors.pink;
  bool isMale = true;

  double _value = 150;
  int weight = 60;
  int age = 25;
  double answer = 10;
  String calc = "CALCULATE";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 12, 9, 34),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Row(
                children: [
                  FemaleBox("MALE", Icons.male),
                  FemaleBox("FEMALE", Icons.female),
                ],
              ),
              Column(children: [
                Container(
                    padding: EdgeInsets.all(32),
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      color: Color.fromARGB(255, 27, 28, 48),
                      borderRadius: BorderRadius.circular(15),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Text("HEIGHT",
                            style: TextStyle(color: Colors.grey, fontSize: 20)),
                        const SizedBox(
                          height: 10,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(_value.toStringAsFixed(0),
                                style: const TextStyle(
                                    fontSize: 45,
                                    color: Colors.white,
                                    fontWeight: FontWeight.w900)),
                            const Text(
                              "cm",
                              style:
                                  TextStyle(fontSize: 20, color: Colors.grey),
                            ),
                          ],
                        ),
                        Slider(
                          min: 100,
                          max: 230,
                          thumbColor: Colors.pink,
                          value: _value,
                          onChanged: (value) {
                            setState(() {
                              _value = value;
                            });
                          },
                        ),
                      ],
                    ))
              ]),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Operation("Weight"),
                  Operation("Age"),
                ],
              ),
              Container(
                decoration: BoxDecoration(
                  color: Colors.pink,
                  borderRadius: BorderRadius.circular(15),
                ),
                padding: EdgeInsets.only(bottom: 5),
                width: MediaQuery.of(context).size.width,
                child: TextButton(
                  child: Text(
                    calc,
                    style: const TextStyle(
                        fontSize: 22,
                        color: Colors.white,
                        fontWeight: FontWeight.w900),
                  ),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => SecondScreen()),

                    );
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  void calculate() {
    answer = (weight / (_value * _value)) * 10000;
    Text(answer.toString(),
        style: const TextStyle(fontSize: 40, color: Colors.white));
    if (calc == "CALCULATE") {
      calc = answer.toStringAsFixed(1);
    } else {
      calc = "CALCULATE";
    }
    setState(() {});
  }

}

I made bmi calculator, I wanna have answer on other screen. I want to send this function calculate() to the second screen, where I will
have the answer of this calculation. I gave Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()), but how to make it work? Thank you in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Make the SecondScreen constructor take a parameter for the type of data that you want to send to it.

 const SecondScreen(
          {Key? key,required this.answer, })
          : super(key: key);
      final String? answer; //define value you want to pass
    
      @override
      _SecondScreenScreenState createState() => _SecondScreenState();
    }

And pass data when navigate

Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => SecondScreen(answer: 'Hello',),
    ));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading