I want this program to do three things. One, display the current value of my number. Two, show how much I have previously entered. Three, enter how much you want to subtract from the original value of the number.
But I am having one problem with my code. In the SubtractState class, I have an onPressed function for my FlatButton. Inside there is a variable called enteredValue. When I try to call this variable in the class 'Type' it's obviously giving me errors.
I know that I need to figure out a way to call a variable from a different class. But how should I do it?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Type()),
);
},
child: Text(
value.toString(),
style: TextStyle(
fontSize: 50.0,
),
),
))));
}
}
class Type extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Text("You Entered ${globals.enteredValue} previously"),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Subtract()),
);
},
)
])))));
}
}
class Subtract extends StatefulWidget {
@override
SubtractState createState() => SubtractState();
}
class SubtractState extends State<Subtract> {
final _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
elevation: 0,
leading: IconButton(
color: Colors.black,
icon: Icon(Icons.arrow_back_ios),
onPressed: () => Navigator.pop(context)),
),
body: Container(
padding: const EdgeInsets.all(30.0),
child: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
textInputAction: TextInputAction.done,
controller: _controller,
keyboardType: TextInputType.number),
FlatButton(
child: Text("Subtract"),
onPressed: () {
//check if we can parse it
if (int.tryParse(_controller.text) == null)
return; //can't parse it
globals.enteredValue = int.parse(_controller.text);
setState(() {
value -= globals.enteredValue;
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyApp()),
);
},
),
])),
))));
}
}
int value = 0;