【问题标题】:The argument type 'FlutterTagging can`t be assigned to the parameter type `Widget`参数类型“FlutterTagging”不能分配给参数类型“Widget”
【发布时间】:2020-01-10 10:08:06
【问题描述】:

我尝试使用外部小部件 (https://github.com/sarbagyastha/flutter_tagging)。我知道,它的保证适用于 android 版本。但是我在 Web 版本中添加它并得到错误“参数类型'FlutterTagging 不能分配给参数类型 Widget`”。它只是简单的代码(例如“Hello, World”,仅此而已)。是不是应该在网页版上工作?

主要代码继承自Widget - 类,源代码清晰。而且我不知道那个错误是什么意思。

    import 'package:flutter_web/material.dart';
    import 'package:flutter_tagging/flutter_tagging.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: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }

    class MyHomePage extends StatelessWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
      final String title;

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'Hello, World!',
                ),
                SingleChildScrollView(
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child:

                        FlutterTagging(
                          textFieldDecoration: InputDecoration(
                              border: OutlineInputBorder(),
                              hintText: "Tags",
                              labelText: "Enter tags"),
                          addButtonWidget: _buildAddButton(),
                          chipsColor: Colors.pinkAccent,
                          chipsFontColor: Colors.white,
                          deleteIcon: Icon(Icons.cancel,color: Colors.white),
                          chipsPadding: EdgeInsets.all(2.0),
                          chipsFontSize: 14.0,
                          chipsSpacing: 5.0,
                          chipsFontFamily: 'helvetica_neue_light',
                          suggestionsCallback: (pattern) async {
                            return await TagSearchService.getSuggestions(pattern);
                          },
                          onChanged: (result) {
                            setState(() {
                              text = result.toString();
                            });
                          },
                        ),
                      ),
                      SizedBox(
                        height: 20.0,
                      ),
                      Center(
                        child: Text(

                          'test',
                          style: TextStyle(color: Colors.pink),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }

    }

    Widget _buildAddButton() {
      return Container(
        padding: EdgeInsets.all(8.0),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(20.0)),
          color: Colors.pinkAccent,
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Icon(
              Icons.add,
              color: Colors.white,
              size: 15.0,
            ),
            Text(
              "Add New Tag",
              style: TextStyle(color: Colors.white, fontSize: 14.0),
            ),
          ],
        ),
      );
    }

【问题讨论】:

  • Flutter 1.9.1+hotfix.1 • 频道测试版 • github.com/flutter/flutter.git 框架 • 修订版 a1fb3fabec(4 天前) • 2019-09-03 18:07:52 -0700 引擎 • 修订版 cc88fa45db 工具• Dart 2.5.0 Flutter Doctor - 好的。

标签: flutter flutter-web


【解决方案1】:

这是因为在 Flutter Web 中,所有小部件都需要是 package:flutter_web/material.dartWidget 类的子类。而您使用的小部件从package:flutter/material.dart 扩展了Widget 类。由于 Flutter 和 Flutter web 现在是独立的项目,您必须更改源代码以使用 package:flutter_web/material.dart 而不是 package:flutter/material.dart。大多数时候,只需替换 import 语句就可以了。

【讨论】:

  • 感谢您的回答。我的错误消失了。但是然后“webdev serve”,我得到了“你依赖于 flutter,flutter_web 技术预览不支持它。有关更多详细信息,请参阅 flutter.dev/web。”想想,它需要等待。
  • 不要使用任何颤振包。如果您想在您的应用程序中使用任何 Flutter 包,请将其源代码和 package:flutter/material.dart 复制粘贴到 package:flutter_web/material.dart
猜你喜欢
  • 2019-12-06
  • 2021-08-23
  • 2021-08-05
  • 2021-10-04
  • 2021-09-13
  • 2021-11-11
  • 2021-07-04
  • 2021-07-06
  • 2021-03-19
相关资源
最近更新 更多