gpt4 book ai didi

function - floatingactionbutton 不会调用刷新

转载 作者:IT王子 更新时间:2023-10-29 07:22:47 25 4
gpt4 key购买 nike

我正在制作一个简单的数学应用程序(请参阅下面的代码)。我现在的问题是为什么我的 float 操作按钮(刷新)不起作用。我已将刷新设置为 changeData,我正在尝试使用我的 widget.refresh 调用该函数。 (最后一行代码)。

稍后我也会尝试实现一些控制:)

一个完全不同的问题,但仍然是关于 flutter 的:关于在一个类中有多少行是可以的?有没有“嘿伙计,这太过分了”?

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(MyHome());

class MyHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'simple math',
theme: ThemeData(primarySwatch: Colors.red),
home: FirstClass(),
);
}
}

class FirstClass extends StatefulWidget {
@override
_FirstClassState createState() => _FirstClassState();
}

class _FirstClassState extends State<FirstClass> {
final random = Random();
int a, b, sum;
String output;

void changeData(String buttonName) {
setState(() {
a = random.nextInt(10);
b = random.nextInt(10);

if (buttonName == '+') {
sum = a + b;
output = '$a + $b = ';
} else if (buttonName == '-') {
if (a >= b) {
sum = a - b;
output = '$a - $b = ';
} else if (b > a) {
//sum cannot be negative here
sum = b - a;
output = '$b - $a = ';
}
}
print(sum.toString());
Navigator.of(context).popUntil(ModalRoute.withName('/'));
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SecondClass(
sum: sum,
refresh: changeData,
output: output,
)));
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(child: Text('+'), onPressed: () =>
changeData('+')),
RaisedButton(child: Text('-'), onPressed: () => changeData('-
')),
],
),
),
);
}
}

class SecondClass extends StatefulWidget {
final int sum;
final String output;

final Function refresh;

SecondClass({this.sum, this.refresh, this.output});

@override
_SecondClassState createState() => _SecondClassState();
}

class _SecondClassState extends State<SecondClass> {
String enterAnswer;
String output = "";

@override
void initState() {
super.initState();
}

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(widget.output),
Container(
width: 50.0,
child: TextField(
keyboardType: TextInputType.number,
onChanged: (val) {
enterAnswer = val;
},
)),
],
),
RaisedButton(
onPressed: () {
if (enterAnswer.isNotEmpty) {
if (enterAnswer == widget.sum.toString()) {
setState(() {
output = 'Correct!';
});
} else {
setState(() {
output = 'Sorry, Worong answer';
});
}
} else {
setState(() {
output = 'You must enter a value';
});
}
},
child: Text('Check Answer'),
),
Text(output),
FloatingActionButton(
child: Icon(Icons.refresh),
onPressed: () {
widget.refresh();
})
],
),
),
),
);
}
}

最佳答案

在您的代码中,您需要将参数传递为 -void changeData(String buttonName) 将字符串作为参数。

工作代码:

FloatingActionButton(
child: Icon(Icons.refresh),
onPressed: () {
widget.refresh('+');
})

更新:正确的方法是同时传递 Button Name 字符串。

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(MyHome());

class MyHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'simple math',
theme: ThemeData(primarySwatch: Colors.red),
home: FirstClass(),
);
}
}

class FirstClass extends StatefulWidget {
@override
_FirstClassState createState() => _FirstClassState();
}

class _FirstClassState extends State<FirstClass> {
final random = Random();
int a, b, sum;
String output;

void changeData(String buttonName) {
setState(() {
a = random.nextInt(10);
b = random.nextInt(10);

if (buttonName == '+') {
sum = a + b;
output = '$a + $b = ';
} else if (buttonName == '-') {
if (a >= b) {
sum = a - b;
output = '$a - $b = ';
} else if (b > a) {
//sum cannot be negative here
sum = b - a;
output = '$b - $a = ';
}
}
print(sum.toString());
Navigator.of(context).popUntil(ModalRoute.withName('/'));
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SecondClass(
sum: sum,
refresh: changeData,
output: output,
buttonName: buttonName,
)));
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(child: Text('+'), onPressed: () => changeData('+')),
RaisedButton(child: Text('-'), onPressed: () => changeData('-')),
],
),
),
);
}
}

class SecondClass extends StatefulWidget {
final int sum;
final String output;
final String buttonName;

final Function refresh;

SecondClass({this.sum, this.refresh, this.output, this.buttonName});

@override
_SecondClassState createState() => _SecondClassState();
}

class _SecondClassState extends State<SecondClass> {
String enterAnswer;
String output = "";

@override
void initState() {
super.initState();
}

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(widget.output),
Container(
width: 50.0,
child: TextField(
keyboardType: TextInputType.number,
onChanged: (val) {
enterAnswer = val;
},
)),
],
),
RaisedButton(
onPressed: () {
if (enterAnswer.isNotEmpty) {
if (enterAnswer == widget.sum.toString()) {
setState(() {
output = 'Correct!';
});
} else {
setState(() {
output = 'Sorry, Worong answer';
});
}
} else {
setState(() {
output = 'You must enter a value';
});
}
},
child: Text('Check Answer'),
),
Text(output),
FloatingActionButton(
child: Icon(Icons.refresh),
onPressed: () {
widget.refresh(widget.buttonName);
})
],
),
),
),
);
}
}

关于function - floatingactionbutton 不会调用刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54773536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com