首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在颤振中使用自定义部件

如何在颤振中使用自定义部件
EN

Stack Overflow用户
提问于 2020-04-14 11:27:58
回答 3查看 1.6K关注 0票数 0

我是新的颤音,我是遵循颤振的官方教程学习颤振的基础知识。

因此,我想创建一个名为"CustomCard“的可重用组件,我这样做了:

代码语言:javascript
复制
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中使用它,我做了以下工作:

代码语言:javascript
复制
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’定义的。

如何解决这个问题?

终端错误:

代码语言:javascript
复制
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 })

编辑:更正拼写错误。还添加了控制台日志。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-04-14 14:46:23

你犯了一些错误,但不要担心它发生在一开始。因此,让我们进入问题:

首先,在主体中定义了一个Center,它只允许在其中包含一个子部件,但是尝试放置两个小部件(Text和CustomCard)。因此,要将这两个小部件都放在一起,您可以将其更改为如下所示:

代码语言:javascript
复制
Center(
      child: Column(
        children: <Widget>[
          Text('centre'),
          CustomCard(...)
        ],
      ),
    ),

此外,请注意,onPress函数以函数为参数,但传递的是print(.)的结果。这是无效的。只需将其更改为:

代码语言:javascript
复制
CustomCard(index: index, onPress: () => print(' this is $index'))

最后,我认为您忽略了定义索引变量。只需添加:

代码语言:javascript
复制
String index = "card1";
票数 1
EN

Stack Overflow用户

发布于 2020-04-14 11:32:41

嘿,请检查这里的更新代码。当您在Center中结束文本和CustomWidget时,会出现几个编译错误,其中它只接受一个子小部件,在onPress方法中也需要进行一些代码更改。

代码语言:javascript
复制
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);
            },
          )
        ],
      ),
    );
  }
}
票数 1
EN

Stack Overflow用户

发布于 2021-02-04 18:08:33

代码语言:javascript
复制
                  child: Center(
                child: Row(
                  children: <Widget>[
                    _box(), //Custom pin view
                    _box(), //Custom pin view
                    _box(), //Custom pin view
                    _box(),//Custom pin view
                  ],
                ),
              )

和自定义小部件(_box)代码

代码语言:javascript
复制
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)),

);}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61206630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档