我想在我的建構式中傳遞一個函式,并想在下一步使用。我也用VoidCallback代替了Function,但同樣的錯誤顯示....。 "引數型別'Function'不能被分配給引數型別'void Function()'"
。 ``` import 'package:flutter/material.dart';
class MyTextField extends StatelessWidget{
const MyTextField({Key? key, required this.label,required.onChanged}) : super(key:
key)。)
final String label。
final VoidCallback onChanged;
@override
Widget build(BuildContext context) {
return TextField(
樣式。const TextStyle(color: Colors.black)。
onChanged: onChanged,
裝飾。 InputDecoration(
hintText: label,
hintStyle: const TextStyle(color: Colors.gray)。
contentPadding。const EdgeInsets.symmetric(垂直。10.0, horizontal: 20.0)。)
邊界。const OutlineInputBorder(
borderRadius: borderRadius.all(Radius.round(32.0))。
),
enabledBorder: const OutlineInputBorder(
borderSide:
borderSide(color: Colors.lightBlueAccent, width: 1.0)。
borderRadius: borderRadius.all(Radius.round(32.0))。
),
focusedBorder: const OutlineInputBorder(
borderSide:
borderSide(color: Colors.lightBlueAccent, width: 2.0) 。
borderRadius: borderRadius.all(Radius.round(32.0))。
),
),
);
}
}
uj5u.com熱心網友回復:
代替
final VoidCallback onChanged;
使用
ValueChanged<String> onChanged。
e.g
class MyTextField extends StatelessWidget{
const MyTextField({Key? key, required this.label,required.onChanged}) : super(key:
key)。)
final String label。
finalValueChanged<String> onChanged;
@override
Widget build(BuildContext context) {
return TextField(
樣式。const TextStyle(color: Colors.black)。
onChanged: onChanged,
裝飾。 InputDecoration(
hintText: label,
hintStyle: const TextStyle(color: Colors.gray)。
contentPadding。const EdgeInsets.symmetric(垂直。10.0, horizontal: 20.0)。)
邊界。const OutlineInputBorder(
borderRadius: borderRadius.all(Radius.round(32.0))。
),
enabledBorder: const OutlineInputBorder(
borderSide:
borderSide(color: Colors.lightBlueAccent, width: 1.0)。
borderRadius: borderRadius.all(Radius.round(32.0))。
),
focusedBorder: const OutlineInputBorder(
borderSide:
borderSide(color: Colors.lightBlueAccent, width: 2.0) 。
borderRadius: borderRadius.all(Radius.round(32.0))。
),
),
);
}
}
uj5u.com熱心網友回復:
因為VoidCallback與text filed中的onChanged型別不一樣,所以
改變
最后 VoidCallback onChanged;
to
final void Function(String)? onChanged;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326628.html
標籤:
