【发布时间】:2020-07-27 02:50:21
【问题描述】:
我是 Flutter 的新手,我正在关注 Flutter 的官方教程来学习 Flutter 的基础知识。
所以,我想创建一个名为“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 说:
没有为类“MyApp”定义方法“CustonCard”。
如何解决?
终端错误:
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 })
编辑:更正了拼写错误。同时添加控制台日志。
【问题讨论】:
-
这能回答你的问题吗? can we create a new widget in Flutter?
-
检查上面相同问题链接的答案链接:stackoverflow.com/a/58199366/7235539
标签: flutter dart flutter-layout