我是新的颤音,我是遵循颤振的官方教程学习颤振的基础知识。
因此,我想创建一个名为"CustomCard“的可重用组件,我这样做了:
class CustomCard extends StatelessWidget {
CustomCard({@required this.index, @required this.onPress});
final index;
final Function onPress;
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: <Widget>[
Text('Card $index'),
FlatButton(
child: const Text('Press'),
onPressed: this.onPress,
)
],
),
);
}
}现在,为了在MyApp中使用它,我做了以下工作:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to flutter',
home: Scaffold(
appBar: AppBar(
title: Text('hello'),
),
body: Center(
child: Text('centre'),
CustomCard(
index:'card1',
onPress: print(' this is $index')
),
),
),
);
}
}现在我的IDE说:
方法'CustonCard‘不是为类'MyApp’定义的。
如何解决这个问题?
终端错误:
Compiler message:
lib/main.dart:17:6: Error: Place positional arguments before named arguments.
Try moving the positional argument before the named arguments, or add a name to the argument.
CustomCard(
^^^^^^^^^^
lib/main.dart:17:6: Error: Expected named argument.
CustomCard(
^
lib/main.dart:15:21: Error: No named parameter with the name '#1'.
body: Center(
^^...
/C:/src/flutter/packages/flutter/lib/src/widgets/basic.dart:1863:9: Context: Found this candidate, but the arguments don't match.
const Center({ Key key, double widthFactor, double heightFactor, Widget child })编辑:更正拼写错误。还添加了控制台日志。
发布于 2020-04-14 14:46:23
你犯了一些错误,但不要担心它发生在一开始。因此,让我们进入问题:
首先,在主体中定义了一个Center,它只允许在其中包含一个子部件,但是尝试放置两个小部件(Text和CustomCard)。因此,要将这两个小部件都放在一起,您可以将其更改为如下所示:
Center(
child: Column(
children: <Widget>[
Text('centre'),
CustomCard(...)
],
),
),此外,请注意,onPress函数以函数为参数,但传递的是print(.)的结果。这是无效的。只需将其更改为:
CustomCard(index: index, onPress: () => print(' this is $index'))最后,我认为您忽略了定义索引变量。只需添加:
String index = "card1";发布于 2020-04-14 11:32:41
嘿,请检查这里的更新代码。当您在Center中结束文本和CustomWidget时,会出现几个编译错误,其中它只接受一个子小部件,在onPress方法中也需要进行一些代码更改。
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to flutter',
home: Scaffold(
appBar: AppBar(
title: Text('hello'),
),
body: Center(
child:Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('centre'),
CustomCard(
index:'card1',
onPress: onPress
)
],
)
)
),
);
}
onPress(index){
print("this is $index");
}
}
class CustomCard extends StatelessWidget {
CustomCard({@required this.index, @required this.onPress});
final index;
final Function onPress;
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: <Widget>[
Text('Card $index'),
FlatButton(
child: const Text('Press'),
onPressed: (){
this.onPress(index);
},
)
],
),
);
}
}发布于 2021-02-04 18:08:33
child: Center(
child: Row(
children: <Widget>[
_box(), //Custom pin view
_box(), //Custom pin view
_box(), //Custom pin view
_box(),//Custom pin view
],
),
)和自定义小部件(_box)代码
Widget _box() {
return Container(
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 3),
alignment: Alignment.center,
height: 50,
width: 50,
child: TextField(
keyboardType: TextInputType.number,
maxLength: 1,
decoration: InputDecoration(border: InputBorder.none, counterText: ''),
),
decoration: BoxDecoration(border: Border.all(color: Colors.blue)),);}
https://stackoverflow.com/questions/61206630
复制相似问题