I'm trying to pass random values between MyFirst class and MySecond class, wich are both stateful.
The code below works fine and you will get two different outputs in the same class depending on wich button you press, but as soon as I try to change one of those final variables to a random variable I get stuck.
Code for random numbers
final random = Random();
int a, b, sum;
void changeData() {
setState(() {
a = random.nextInt(10);
b = random.nextInt(10);
sum = a + b;
});
}
My present code looks like this
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flight 4',
theme: ThemeData(primarySwatch: Colors.red),
home: MyFirst(),
routes: <String, WidgetBuilder>{
'/screen1': (BuildContext context) => MyFirst(),
'/screen2': (BuildContext context) => MySecond(),
},
);
}
}
class MyFirst extends StatelessWidget {
String PassValue = "3 + 2 = "; //Trying to get this integers to random
String PassValue2 = "4 - 1 = "; //Trying to get this integers to random
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('FLIGHT 4'),
),
body: Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.deepOrangeAccent,
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("+"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
MySecond(passedValue: PassValue),
));
}),
RaisedButton(
child: Text("-"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
MySecond(passedValue: PassValue2),
));
}),
],
),
),
),
);
}
}
class MySecond extends StatelessWidget {
var passedValue;
MySecond({this.passedValue});
@override
Widget build(BuildContext context) {
return Container(
child: Text(passedValue),
);
}
}
Aucun commentaire:
Enregistrer un commentaire