带有参数的Flutter StatefulWidget

时间:2019-12-19 07:45:46

标签: flutter dart

我是Flutter的新手。我有一个StatefulWidget和一个default parameter,我已经完成了研究,许多人都用vivid这样的例子izwebtechnologiesStackOverFlow回答了这个问题我的代码正常工作。它给了我错误

Invalid argument(s)
The relevant error-causing widget was: 
Gallery file:///xxx/xxx/xxx/xxx/lib/main.dart:241:13

我的第214行看起来像

  var images = new List<String>();
  .
  .
  .
  class NewGallery extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _scafoldkey = GlobalKey<ScaffoldState>();
    return Scaffold(
      key: _scafoldkey,
      backgroundColor: Colors.transparent,
     //Where the exception is occurring from main.dart:241:13
      body: Gallery(images),
    );
  }
}

我的画廊课

 class Gallery extends StatefulWidget {

  final List<String> images;
  const Gallery(this.images);

  @override
  State createState() => new _GalleryState();
 }

我的画廊州立课程

class _GalleryState extends State<Gallery> {
      int currentImageIndex;

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.black,
          body: new Dismissible(
            //resizeDuration: null,
            onDismissed: (DismissDirection direction) {
              setState(() {
                currentImageIndex +=
                    direction == DismissDirection.endToStart ? 1 : -1;
                if (currentImageIndex < 0) {
                  currentImageIndex = widget.images.length - 1;
                } else if (currentImageIndex >= widget.images.length) {
                  currentImageIndex = 0;
                }
              });
            },
            key: new ValueKey(currentImageIndex),
            child: SizedBox.expand(
                child: Container(
                    color: Colors.black,
                    margin: const EdgeInsets.only(left: 0, top: 40),
                    child: FadeInImage.assetNetwork(
                        fit: BoxFit.fitWidth,
                        placeholder: "assets/images/placeholder.png",
                        image: widget.images.elementAt(currentImageIndex)))),
          ),
        );
      }
    }

请注意,我是个新手,仍然不了解很多内容,因此我的代码可能没有得到优化。

2 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
设计错误,您需要使用ListView,在这种情况下,currentImageIndex将不起作用

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

//var images = new List<String>();
List<String> images = [
  "https://picsum.photos/250?image=9",
  "https://picsum.photos/250?image=9"
];

class NewGallery extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _scafoldkey = GlobalKey<ScaffoldState>();
    return Scaffold(
      key: _scafoldkey,
      backgroundColor: Colors.transparent,
      //Where the exception is occurring from main.dart:241:13
      body: Gallery(images),
    );
  }
}

class Gallery extends StatefulWidget {
  final List<String> images;
  Gallery(this.images);

  @override
  State createState() => new _GalleryState();
}

class _GalleryState extends State<Gallery> {
  int currentImageIndex;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.black,
      body: ListView.builder(
        itemCount: images.length,
        itemBuilder: (BuildContext context, int index) {
          return Dismissible(
            onDismissed: (DismissDirection direction) {
              setState(() {
                images.removeAt(index);
              });
            },
            secondaryBackground: Container(
              child: Center(
                child: Text(
                  'Delete',
                  style: TextStyle(color: Colors.white),
                ),
              ),
              color: Colors.red,
            ),
            background: Container(),
            child: Container(
                color: Colors.black,
                margin: const EdgeInsets.only(left: 0, top: 40),
                child: FadeInImage.assetNetwork(
                    fit: BoxFit.fitWidth,
                    placeholder: "assets/images/placeholder.png",
                    image: widget.images[index])),
            key: UniqueKey(),
            direction: DismissDirection.endToStart,
          );
        },
      ),
    );
  }
}

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: NewGallery(),
    );
  }
}

答案 1 :(得分:-1)

尝试在Gallery构造函数有状态小部件中删除const。

class Gallery extends StatefulWidget {

  final List<String> images;
  Gallery(this.images);

  @override
  State createState() => new _GalleryState();
}