0

I'm absolutely new to flutter and still learning. But couldn't wait to finish my first app until learn all. And also I'm new to programming too.

  • I'm creating a calculator
  • Multiply two random numbers and display the value on a text field (an inactive text field)

Actually I need to save that result from that text field with random names to a list on another page.

I've already created that page and can navigate to it using another button.

  1. When I press the "Save" button I need to open a popup.
  2. On that popup I need to add different names
  3. Then I need to save it into that list of another page with current value of the text field.

I have went through a lot of resources and tutorials. But unable to find the answer and still I'm searching.

Optional: I only found how to display the answer on a text field yet. So, that's why I'm using a text field here. And I'm really happy to know about another widgets to display the answer if you can.

The text field code

            Padding(
              padding: const EdgeInsets.fromLTRB(50.0, 50.0, 50.0, 0),
              child:
              TextField(
                controller: volume,
                decoration: InputDecoration(
                  prefixIcon: Icon((Icons.question_answer),),
                  hintText: answer,
                  hintStyle: TextStyle(fontSize: 26.0),
                  labelText: "Volume",
                  labelStyle: TextStyle(fontSize: 18.0),
                  floatingLabelBehavior: FloatingLabelBehavior.always,
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(20.0),),
                ),
                cursorColor: Colors.grey,
                textAlign: TextAlign.center,
                keyboardType: TextInputType.number,
                maxLength: 12,
                maxLines: 1,
                textInputAction: TextInputAction.next,
                style: TextStyle(fontSize: 20.0),
                enabled: false,
              ),
            ),

The calculate button code

            Padding(
              padding: const EdgeInsets.fromLTRB(50.0, 20.0, 50.0, 20.0),
              child: ButtonTheme(
                minWidth: 300.0,
                height: 60.0,
                child: RaisedButton(
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                  child: const Text('Calculate',style: TextStyle(fontSize: 20.0),),
                  color: Colors.blue,
                  textColor: Colors.white,
                  onPressed: (){
                    setState(() {
                      double multiply = constant * double.parse(length.text) * double.parse(perimeter.text) * double.parse(perimeter.text);
                      answer = multiply.toStringAsFixed(2);
                    });
                  },
                ),
              ),
            ),

And the "Save" button code

            Padding(
              padding: const EdgeInsets.fromLTRB(50.0, 10.0, 50.0, 0.0),
              child: ButtonTheme(
                minWidth: 220.0,
                height: 50.0,
                child: RaisedButton(
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                  child: const Text('Save',style: TextStyle(fontSize: 20.0),),
                  color: Colors.blueGrey,
                  textColor: Colors.white,
                  onPressed: (){},
                ),
              ),
            ),

Thank you so much!

1 Answers1

0

maintain that list in a separate class,


class ClassWithList{
  List list = [];
  ClassWithList();
}

class Page1 extends StatelessWidget {
  final ClassWithList classWithList;

  const Page1({Key key, this.classWithList}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

class Page2 extends StatelessWidget {
  final ClassWithList classWithList;

  const Page2({Key key, this.classWithList}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

then you can access the same object in both pages

  final ClassWithList classWithList = ClassWithList();
  var w = Page1(classWithList: classWithList,);
  var w2 = Page2(classWithList: classWithList,);

further if you use Provider you can skip passing the class instance

Yadu
  • 2,979
  • 2
  • 12
  • 27