【问题标题】:How to use Custom Widgets in flutter如何在颤振中使用自定义小部件
【发布时间】: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 })

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

【问题讨论】:

标签: flutter dart flutter-layout


【解决方案1】:

你有一些错误正在发生,但不要担心它在一开始就发生。那么让我们进入问题:

首先,在正文中,您定义了一个 Center 小部件,该小部件只允许在其中包含一个孩子,但您尝试放置两个小部件(Text 和 自定义卡)。因此,要放置两个小部件,您可以将其更改为以下内容:

Center(
      child: Column(
        children: <Widget>[
          Text('centre'),
          CustomCard(...)
        ],
      ),
    ),

此外,请注意 onPress 函数将 Function 作为参数,但您传递的是 print(...) 的结果,即 void。只需将其更改为:

CustomCard(index: index, onPress: () => print(' this is $index'))

最后,我认为您错过了定义 index 变量。只需添加:

String index = "card1";

【讨论】:

  • 我很高兴它有帮助。
【解决方案2】:

嘿,请在此处查看更新的代码。当您将 Text 和 CustomWidget 都放在 Center 中时,会出现一些编译错误,它只接受一个子小部件,并且在 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);
            },
          )
        ],
      ),
    );
  }
}

【讨论】:

  • 编辑:更正了拼写错误。另外,在我的查询中添加错误。
  • 莱昂纳多的回答对我有用。虽然,你指出了同样的问题。
【解决方案3】:
                  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)),

); }

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 2020-09-07
    • 2018-09-13
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2019-12-20
    • 2019-05-10
    • 1970-01-01
    相关资源
    最近更新 更多