【问题标题】:add data to listview when clicked flutter单击颤动时将数据添加到列表视图
【发布时间】:2020-12-24 22:31:03
【问题描述】:

我的应用中有一个列表视图。当我单击项目时,如果项目有任何数据,我想要显示公司名称,如果没有数据,我想保存选定的项目并移至下一页。如果有数据我想打开我的卡并将其显示为列表。 (在同一页上) 我怎样才能得到它根据公司名称开卡?是否可以在 ONTAP 中添加新的小部件?

这是我的代码,

class _CountryScreenState extends State<CountryScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 12.0),
            child: Text('Please chose',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),),
          ),
          Expanded(
            child: Center(
              child: FutureBuilder<CountryList>(
                future: fetchCountryList(),
                builder: (BuildContext context, AsyncSnapshot<CountryList> snapshot) {
                  if (snapshot.hasData) {
                    return ListView.builder(
                      itemCount: snapshot.data.data.length,
                      itemBuilder: (BuildContext context, int position) {
                        return Container(
                          height: 72,
                          padding: const EdgeInsets.all(10.0),
                          child: Card(
                            child: InkWell(
                              onTap: () async {
                                List<Company> companies= await fetchCompaniesList(snapshot.data.data[position].code);
                                Navigator.pushNamed(context, MainScreen.routeName);

                              },
                              child: Row(
                                children: [
                                  Padding(
                                    padding:
                                        const EdgeInsets.symmetric(horizontal: 8.0),
                                    child: Image.asset(
                                      'icons/flags/png/${snapshot.data.data[position].code.toLowerCase()}.png',
                                      package: 'country_icons',
                                      height: 20,
                                      width: 34,
                                    ),
                                  ),
                                  Padding(
                                    padding:
                                        const EdgeInsets.symmetric(horizontal: 8.0),
                                    child: Text(snapshot.data.data[position].name,style: TextStyle(fontSize: 16),),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        );
                      },
                    );
                  } else {
                    return CircularProgressIndicator();
                  }
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: api flutter listview


    【解决方案1】:

    要随时添加小部件,您可以直接将文档添加到 firebase,并使用 StreamBuilder 而不是 FutureBuilder。

    您可以在要扩展卡片时添加详细信息吗?

    【讨论】:

    • 当然,如果我有任何公司名称,我想下拉我的卡片小部件并列出公司名称。然后用户选择这些公司名称之一并导航新页面
    • 如果我使用 streambuilder 会有什么变化?
    • 如果您使用流构建器,那么当您将新对象添加到 firebase (onTap) 时,它将直接在您的应用中,因此它可以在 Tap 上添加卡片。
    • 对于下拉部分,我现在将使用 Future Builder,并将其添加到 Top of the Container 或 Card ,这将获取公司列表,以便您可以使用它来构建卡与所有公司合作,并建立您的扩展卡
    • 你说如果我要使用streambuilder,我需要将FutureBuilder添加到它吗?(因为我的下拉部分?)我不完全理解解决方案:/
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 2020-07-18
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    相关资源
    最近更新 更多