颤动+ PhotoView适合屏幕-布局期间_RenderLayoutBuilder对象具有无限大小

时间:2019-12-25 04:49:15

标签: flutter

我希望能够显示可缩放的图像,以初始填充屏幕宽度并保持宽高比, 我还需要屏幕可滚动(SingleChildScrollView),因为它将包含除图像之外的其他元素

附件完整的应用程序是由

生成的
  1. flutter create --androidx view_photo

  2. 添加吸气剂

代码:

  get myImage {
    var photoView = PhotoView(
      initialScale: PhotoViewComputedScale.covered,
      imageProvider: AssetImage("assets/1.jpg"),
    );
    return Container(
      height: 100, // set auto according to image height
      child: photoView);
  }
  1. myImage添加到Column

问题:我希望图像的高度根据图像的大小而定,以便在显示容器时,容器的宽度/高度将根据图像的长宽比设置为最大值。

如果我取消高度,应用程序将抛出错误:

_RenderLayoutBuilder object was given an infinite size during layout.

我想我可以根据图像长宽比和屏幕宽度(如何?)来计算高度,但是我更喜欢一种更简单的方法来实现这一目标。

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.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(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  get myImage {
    var photoView = PhotoView(
      initialScale: PhotoViewComputedScale.covered,
      imageProvider: AssetImage("assets/1.jpg"),
    );
    return Container(height: 100, child: photoView);
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            myImage,
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

注意:如果您为此感到苦恼,那么可以简单地通过:在新屏幕中打开图像

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:photo_view/photo_view.dart';

class ReceiptImgZoomable extends StatefulWidget {
  String renderUrl;
  ReceiptImgZoomable(this.renderUrl) : super();

  @override
  _ReceiptImgZoomableState createState() => _ReceiptImgZoomableState();
}

class _ReceiptImgZoomableState extends State<ReceiptImgZoomable> {
  @override
  Widget build(BuildContext context) {
    // var image = Image.file(File(widget.renderUrl));
    var image = PhotoView(
      imageProvider: FileImage(File(widget.renderUrl)),
    );
    return Container(
      child: image,
    );
  }
}

和:

  @override
  Widget build(BuildContext context) {
    // InkWell is a special Material widget that makes its children tappable
    // and adds Material Design ink ripple when tapped.
    return GestureDetector(
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              // builder methods always take context!
              builder: (context) {
                return ReceiptImgZoomable(widget.renderUrl);
              },
            ),
          );
        },
        child: receiptImage);
  }

1 个答案:

答案 0 :(得分:0)

要简单地处理显示图像的放大和缩小,您可以使用 Flutter 的 InteractiveViewer