简体   繁体   中英

InteractiveViewer class in flutter is not defined

I was reading about InteractiveViewer from here https://api.flutter.dev/flutter/widgets/InteractiveViewer-class.html

But when I tried to use InteractiveViewer in my code It gave me an error:-

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

My flutter code is below:-

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],
            ),
          ),
        ),
      ),
    );
  }
}

My flutter version is Flutter 1.17.5 and dart version is Dart 2.8.4

Can you please tell me what is the problem here.

The problem is that you are using an old version of flutter.

InteractiveViewer was released in 1.20 version of Flutter

So you need to upgrade your flutter, To upgrade you can use the following command in the command-prompt:-

flutter upgrade 

This command gets the most recent version of the Flutter SDK that's available on your current Flutter channel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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