简体   繁体   English

Widget Classes Arguments 如何作为道具在 Flutter 中创建具有行和列的动态 Widget?

[英]How can Widget Classes Arguments be used as props to create dynamic Widgets with Row and Columns in Flutter?

I am revisiting Flutter development after working with React after some times and I was trying to implement something as passing props across multiple components, in order to create dynamic Widgets (components) that render based on props.在使用 React 一段时间后,我正在重新审视 Flutter 开发,并且我试图实现一些东西作为跨多个组件传递道具,以便创建基于道具呈现的动态小部件(组件)。 I could somehow achieve the result but still I cannot understand something.我可以以某种方式达到结果,但我仍然无法理解。

So, in this example, I am creating a Widget Class which accepts text as an argument, and I am able to use the value of the text below in the Text return method:因此,在此示例中,我正在创建一个小部件 Class,它接受文本作为参数,并且我能够在 Text 返回方法中使用以下文本的值:


class NeoText extends StatelessWidget {
  final String text;

  const NeoText({ 
    super. Key,
    required this.text,
  });

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: const TextStyle(
        color: Colors.white,
        fontSize: 15.0,
        fontWeight: FontWeight.bold,
        letterSpacing: 1.0,
        fontFamily: 'Lato',
      ),
    );
  }
}

And I can use the Widget (component) that I created in my main.dart file like:我可以使用在main.dart文件中创建的 Widget(组件),例如:

...
...
    child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: const [
                  NeoText(
                    text: "⚽️Goals: 2",
                  ),
                  NeoText(
                    text: "🅰️Assists: 6",
                  ),
                  NeoText(
                    text: "🧤Saves: 26",
                  ),
                ],
              ),
...
...

But when I want to create a more complex Custom Widget that accepts multiple childs and other arguments I cannot seem to make it work.但是当我想创建一个更复杂的自定义小部件来接受多个孩子和其他 arguments 时,我似乎无法让它工作。

Here is an example.这是一个例子。 I am creating this CustomCard widget which can have multiple widgets in its structure, including a Text widget.我正在创建这个 CustomCard 小部件,它的结构中可以有多个小部件,包括一个文本小部件。 Now here I am doing the same thing, I am creating a variable named title, and trying to pass it in the Text Widget, so when I use this component in my main.dart file I can provide the title name and make the Card different from other cards.现在我在这里做同样的事情,我正在创建一个名为 title 的变量,并尝试在 Text Widget 中传递它,所以当我在我的main.dart文件中使用这个组件时,我可以提供标题名称并使卡片不同从其他卡。 But on return method of Text I get an error (attached after the code below):但是在 Text 的返回方法上我得到一个错误(附在下面的代码之后):

import 'package:flutter/material.dart';

class CustomCard extends StatelessWidget {
  final String? title;

  const CustomCard({super.key, @required this.title});

  @override
  Widget build(BuildContext context) {
    // Material is a conceptual piece
    // of paper on which the UI appears.
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(3.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Icon(
              Icons.sports_soccer,
              color: Colors.black,
              size: 50.0,
            ),
            SizedBox(
              height: 8.0,
            ),
            Text(
              title!,
              style: TextStyle(
                color: Colors.black,
                fontSize: 15.0,
                fontWeight: FontWeight.bold,
                letterSpacing: 1.0,
                fontFamily: 'Lato',
              ),
            ),
            SizedBox(
              height: 8.0,
            ),
            Text(
              '26',
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
                fontSize: 15.0,
                letterSpacing: 1.0,
                fontFamily: 'Lato',
              ),
            ),
          ],
        ),
      ),
    );
  }
}

I am wondering now, why this is not working considering that the first example works just the way I want.我现在想知道,考虑到第一个示例按我想要的方式工作,为什么这不起作用。

错误截图小部件道具

Another challenge that I am having also is, to render as many children as I provide to my Costum Method.我还面临的另一个挑战是,为我的 Costum Method 提供尽可能多的孩子。 So lets say, I want create my own Custom Widget that accepts a dynamic number of children to render.因此,可以说,我想创建自己的自定义小部件,它接受动态数量的子级进行渲染。 In other words, we can also say that, I want to create a custom Row component that renders as many children as I provide.换句话说,我们也可以说,我想创建一个自定义的 Row 组件,它可以呈现我提供的尽可能多的子组件。 Here is an example of what I am trying to achieve is:这是我想要实现的一个示例:

I want that in my main I render this kind of Custom Widget, that renders a number of children inside it:我希望在我的主中渲染这种自定义小部件,在其中渲染许多子小部件:

              children: [
                  NeoText(
                    text: "⚽️Goals: 2",
                  ),
                  NeoText(
                    text: "🅰️Assists: 6",
                  ),
                  NeoText(
                    text: "🧤Saves: 26",
                  ),
                ],
              ),
            ),

I want to make the ScoreRow Widget a Custom Widget that accepts multiple children inside it and renders as many children as provided like in the below example.我想让 ScoreRow 小部件成为一个自定义小部件,它接受其中的多个子级并呈现与下面示例中提供的一样多的子级。 Is it possible to do something like that?有可能做这样的事情吗? Or is there any concept I need to cover?或者有什么我需要介绍的概念吗?

import 'widgets/neo_text.dart';

class ScoreRow extends StatelessWidget {
  final Widget? children;

  const ScoreRow({super.key, required this.children});

  @override
  Widget build(BuildContext context) {
    return (
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [children!],
          // add the children here
      )
    );
     
  }
}```

Try to remove const keyword尝试删除const关键字

 Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: const [

this:这个:

Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [

In order to have a widget that is compile-time constant, all of its properties should also be compile-time constants.为了拥有一个compile-time时常量的小部件,它的所有属性也应该是编译时常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “如何在一个行小部件中创建两个文本小部件” - “How to create two text widgets in a row widget” 如何在 Flutter 中创建动态 Widget - How to Create dynamic Widget in Flutter 如何在 Flutter 中连续使用多个小部件的方形小部件 - How to have a square shaped widget in a row with multiple widgets in Flutter 如何在单行小部件上左对齐、居中、右对齐 flutter - How to align widgets left, center, right on single row widget flutter 为什么 flutter 将其 CLASSES 称为 WIDGETS? 为什么不称它为 class? 为什么使用“小部件”一词? - Why does flutter call its CLASSES as WIDGETS? Why not call it a class? why the term "widget" is used? 如何创建所有类和小部件都可以在 Flutter 中使用的公共数据源 - How to create a common source of data that all classes and widgets can use in flutter 如何更改行小部件内小部件的堆栈顺序? - How can I change the stack order of widgets inside a row widget? 如何通过 Flutter Provider 创建和更新 Dynamic Widgets 的值 - How to create and update the value of Dynamic Widgets through Flutter Provider Flutter 小部件在行中的动态高度 - Flutter Dynamic Height of Widget in Row 如何在 ListView 中的 flutter 中创建一行可滚动的文本框或小部件? - how to create a row of scrollable text boxes or widgets in flutter inside a ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM