Function 当我将函数发送给它时,它不会执行

Function 当我将函数发送给它时,它不会执行,function,flutter,flutter-widget,Function,Flutter,Flutter Widget,所以这个函数 Buttons( '1', () { setState(() { visibleText = '1'; }); }, ), 当我按下按钮时不起作用,但如果我这样做,它会起作用: Expanded( child: RawMaterialButton(

所以这个函数

Buttons(
         '1',
          () {
             setState(() {
               visibleText = '1';
                 });
                },
               ),
当我按下按钮时不起作用,但如果我这样做,它会起作用:

Expanded(
          child: RawMaterialButton(
            onPressed: () {
              setState(() {
               visibleText = '1';
                 });
            },
            child: Card(
              child: Center(
                child: Text(
                  '1',
                  style: TextStyle(fontSize: 65),
                ),
              ),
            ),
          ),
        );
有什么我不明白的吗。对不起,如果缩进是错的

const.dart文件只有一个fontSize变量,所以我不必附加它

我还在按钮“1”中添加了一条打印语句,但它没有打印,因此似乎该功能没有在压力机上执行

import 'package:flutter/material.dart';
import 'package:calculator/const.dart';

void main() {
  runApp(MyApp());
}
String visibleText = '0';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    int computationalCount;

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text('Calculator'),
          ),
        ),
        body: Column(
          children: [
            Expanded(
              child: Container(
                alignment: AlignmentDirectional.topEnd,
                child: Text(
                  visibleText,
                  style: TextStyle(fontSize: fontSize),
                ),
              ),
            ),
            Row(
              children: [
                Buttons(
                  'C',
                  () {
                    visibleText = '';
                    computationalCount = 0;
                  },
                ),
                Buttons(
                  '( )',
                  () {
                    int num = 0;
                    if (num / 2 == 0) {
                      visibleText += '(';
                    } else {
                      visibleText += ')';
                    }
                  },
                ),
                Buttons(
                  '^',
                  () {},
                ),
                Buttons(
                  '<=',
                  () {},
                ),
              ],
            ),
            Row(
              children: [
                Buttons(
                  '1',
                  () {
                    setState(() {
                      visibleText = '1';
                    });
                  },
                ),
                Buttons(
                  '2',
                  () {},
                ),
                Buttons(
                  '3',
                  () {},
                ),
                Buttons(
                  '+',
                  () {},
                ),
              ],
            ),
            Row(
              children: [
                Buttons(
                  '4',
                  () {},
                ),
                Buttons(
                  '5',
                  () {},
                ),
                Buttons(
                  '6',
                  () {},
                ),
                Buttons(
                  '-',
                  () {},
                ),
              ],
            ),
            Row(
              children: [
                Buttons(
                  '7',
                  () {},
                ),
                Buttons(
                  '8',
                  () {},
                ),
                Buttons(
                  '9',
                  () {},
                ),
                Buttons(
                  '×',
                  () {},
                ),
              ],
            ),
            Row(
              children: [
                Buttons(
                  '.',
                  () {},
                ),
                Buttons(
                  '0',
                  () {},
                ),
                Buttons(
                  '÷',
                  () {},
                ),
                Buttons(
                  '=',
                  () {},
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class Buttons extends StatelessWidget {
  final String text;
  final onPress;
  const Buttons(this.text, this.onPress);
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: RawMaterialButton(
        onPressed: () {
          // ignore: unnecessary_statements
          onPress;
        },
        child: Card(
          child: Center(
            child: Text(
              text,
              style: TextStyle(fontSize: 65),
            ),
          ),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:计算器/常数镖”;
void main(){
runApp(MyApp());
}
字符串visibleText='0';
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
整数计算账户;
返回材料PP(
家:脚手架(
appBar:appBar(
标题:中心(
子项:文本(“计算器”),
),
),
正文:专栏(
儿童:[
扩大(
子:容器(
对齐:对齐方向.topEnd,
子:文本(
可见文本,
样式:文本样式(fontSize:fontSize),
),
),
),
划船(
儿童:[
钮扣(
"C",,
() {
visibleText='';
计算量=0;
},
),
钮扣(
'( )',
() {
int num=0;
如果(num/2==0){
visibleText+='(';
}否则{
visibleText+=')';
}
},
),
钮扣(
'^',
() {},
),
钮扣(

“您需要修改Buttons小部件以使其接收一个手势回调,如下所示:

类按钮扩展了无状态小部件{
最终字符串文本;
final GestureTapCallback onPressed;//在此处添加类型
常量按钮(this.text、this.onPress);
@凌驾
小部件构建(构建上下文){
扩大回报(
子项:RawMaterialButton(
onPressed:onPressed,//onPressed现在应该是这样的
孩子:卡片(
儿童:中心(
子:文本(
文本,
样式:TextStyle(字体大小:65),
),
),
),
),
);
}

请澄清您的问题,您正在调用一个类似于按钮的小部件,我看不出问题是什么,请尝试查看最佳实践指南定义属性类型,最终函数onPress;它不起作用,没有任何更改