繁体   English   中英

未定义 flutter 中的 InteractiveViewer class

[英]InteractiveViewer class in flutter is not defined

我从这里阅读有关 InteractiveViewer https://api.flutter.dev/flutter/widgets/InteractiveViewer-class.html

但是当我尝试在我的代码中使用 InteractiveViewer 时,它给了我一个错误:-

The method 'InteractiveViewer' isn't defined for the type 'MyHomeState'.
Try correcting the name to the name of an existing method, or defining a method named 
'InteractiveViewer'.dartundefined_method

我的 flutter 代码如下:-

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  static const String _title = "InteractiveViewer Sample";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(
          title: const Text(_title),
        ),
        body: MystateLessWidget(),
      ),
    );
  }
}

class MystateLessWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: MyHome());
  }
}

class MyHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyHomeState();
  }
}

class MyHomeState extends State<MyHome> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: InteractiveViewer(
        boundaryMargin: EdgeInsets.all(20.0),
        minScale: 0.1,
        maxScale: 1.6,
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: <Color>[Colors.orange, Colors.red],
              stops: <double>[0.0, 1.0],
            ),
          ),
        ),
      ),
    );
  }
}

我的 flutter 版本是Flutter 1.17.5和 dart 版本是Dart 2.8.4

你能告诉我这里有什么问题吗?

问题是您使用的是旧版本的 flutter。

InteractiveViewer 发布于1.20 version of Flutter

因此,您需要升级您的 flutter,要升级您可以在命令提示符中使用以下命令:-

flutter upgrade 

此命令获取当前 Flutter 通道上可用的 Flutter SDK 的最新版本。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM