未定义Flutter中的InteractiveViewer类

时间:2020-08-08 17:28:13

标签: flutter

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

开始阅读有关InteractiveViewer的信息

但是当我尝试在代码中使用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

我的颤动代码如下:-

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 1.17.5 飞镖版本为Dart 2.8.4

能告诉我这里是什么问题。

1 个答案:

答案 0 :(得分:2)

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

InteractiveViewer已在1.20 version of Flutter中发布

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

flutter upgrade 

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