Flutter:动态窗口小部件作为另一个窗口小部件(B)的参数(A),该窗口小部件(A)接收在窗口小部件B上设置的参数

时间:2020-07-18 18:57:38

标签: flutter widget arguments

首先,对标题感到抱歉,听起来很杂乱,但是我希望有人已经遇到了这个查询。将尝试在这里解释我要实现的目标。

我正在尝试重用futureBuilder小部件,因为我的应用程序有很多列表。

//flutter
import 'package:flutter/material.dart';

class StreamBuilderWidget extends StatelessWidget {
  Stream streamFunction;
  ScrollController scrollController;
  Widget customWidget;

  StreamBuilderWidget({
    this.streamFunction,
    this.scrollController,
    this.customWidget,
  });

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: streamFunction,
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
          case ConnectionState.none:
            return Center(child: CircularProgressIndicator());
          case ConnectionState.active:
          case ConnectionState.done:
            if (snapshot.hasData) {
              return IgnorePointer(
                child: ListView.builder(
                  controller: scrollController,
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    final item = snapshot.data[index];
                    return customWidget(item);
                        //PostItem(item) //example
                        //VideosItem(item) //example
                        //BooksItem(item) //example
                        ;
                  },
                ),
              );
            }
        }
        return Center(
          child: CircularProgressIndicator(),
        );
      },
    );
  }
}


头痛来自参数Widget,我需要接收它自己的参数“ item”。

虽然我读不懂它,但是读起来似乎很容易。

一个讨厌的解决方法是将小部件参数替换为字符串参数,并将其与switch语句一起使用:

switch (widgetType) {
    case 'PostItemWidget':
        return PostItemWidget(post: item);
    case 'YouTubeItemWidget':
        return VideosItem(video: item)
    ... //and so on

您明白了,无论如何,使它成为一个更加切肉刀/更干净的解决方案是很好的选择。

让我知道您是否需要任何其他输入。 预先感谢。

1 个答案:

答案 0 :(得分:0)

代码更新。这样使用

final item = snapshot.data
customWidget(item[index]['name'])

import 'package:flutter/material.dart';

class StreamBuilderWidget extends StatelessWidget {
  Stream streamFunction;
  ScrollController scrollController;
  Widget customWidget;

  StreamBuilderWidget({
    this.streamFunction,
    this.scrollController,
    this.customWidget,
  });

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: streamFunction,
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
          case ConnectionState.none:
            return Center(child: CircularProgressIndicator());
          case ConnectionState.active:
          case ConnectionState.done:
            if (snapshot.hasData) {
              return IgnorePointer(
                child: ListView.builder(
                  controller: scrollController,
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    final item = snapshot.data;
                    return customWidget(item[index]['name']); ///use like that
                        //PostItem(item[index]) //example
                        //VideosItem(item[index]) //example
                        //BooksItem(item[index]) //example
                        ;
                  },
                ),
              );
            }
        }
        return Center(
          child: CircularProgressIndicator(),
        );
      },
    );
  }
}