簡體   English   中英

Flutter:將 function 作為參數傳遞給 Stateful Widget Class

[英]Flutter: Passing a function as parameter to a Stateful Widget Class

我有一個有狀態的小部件,它返回一個腳手架,如下所示:

class TimerPage extends StatefulWidget {
  const TimerPage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class TimerPageState extends State<TimerPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          getMaterialTextButton('1', 'Roboto', 24, keypadPressed('1')),   /// ERROR
          getMaterialTextButton('2', 'Roboto', 24, keypadPressed2('2')),  /// ERROR
        ]
      ),
    );
  }
}

所以我想做的是在getMaterialTextButton()中傳遞一個通用的 function ,它將返回一個材質按鈕,如下所示:

// this function is within the TimerPageState class
Widget getMaterialTextButton(String text, String fontname, double fontsize, Function onPressAction) {
  return (
    MaterialButton(
      onPressed: () {
        onPressAction(text);
      },
      color: Colors.blue,
      textColor: Colors.white,
      child: Text(
          text,
          style: TextStyle(fontFamily: fontname, fontSize: fontsize)
      ),
      padding: const EdgeInsets.all(24),
      shape: const CircleBorder(),
    )
  );
}

// this function is to be called when the button 1 is pressed
// also resides inside TimerPageState class
void keyPressed (String text) {
   print('Pressed: $text');
}

// this function is also to be called when button 2 is pressed
// also resides inside TimerPageState class
void keyPressed2 (String text) {
   print('Pressed: $text');
}

但這似乎不起作用,因為 dart 給我一個異常: Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'Function') Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'Function') 我怎樣才能正確地做這個操作?

您傳遞的是 function 的返回值,而不是 function 本身

改變這個

getMaterialTextButton('1', 'Roboto', 24, keypadPressed('1')),

對此

getMaterialTextButton('1', 'Roboto', 24, keypadPressed),

首先,您應該更喜歡 function 類型注釋中的完整簽名https://dart.dev/guides/language/effective-dart/design#prefer-signatures-in-function-type-annotations

所以

Widget getMaterialTextButton(String text, String fontname, double fontsize, Function onPressAction) {...}

應該

Widget getMaterialTextButton(String text, String fontname, double fontsize, void Function(String) onPressAction) {...}

其次,這不是傳入 function:

getMaterialTextButton('1', 'Roboto', 24, keypadPressed('1')),

那就是調用keypadPressed並傳入 function 的結果,它看起來是void 刪除括號,不要將任何內容傳遞給keypadPressed , getMaterialTextButton 的主體是應該調用getMaterialTextButton的地方。

getMaterialTextButton('1', 'Roboto', 24, keypadPressed),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM