简体   繁体   English

Flutter 带有小部件列表的 Swiper

[英]Flutter Swiper with a list of widgets

I'm creating an application where there is a scroll view made up using Flutter_Swiper .我正在创建一个应用程序,其中有一个使用Flutter_Swiper组成的滚动视图。 The problem is that I can't figure out how to put a list of widget inside the Swiper.问题是我不知道如何将小部件列表放入 Swiper 中。 Let me explain with an example:让我用一个例子来解释:

  Widget build(BuildContext context) {
  return new Container(
     child: new Swiper(
       itemBuilder: (BuildContext context, int index) {
          return MyWidget(id: 1, text: "hello");

       },
     itemCount: 10,
     viewportFraction: 0.8,
     scale: 0.85,
     )
  );
  }

This is the code that the official wiki has provided to me, it works but obviously it shows me the same widget every times.这是官方 wiki 提供给我的代码,它可以工作,但显然它每次都向我显示相同的小部件。
So, to do this, I created a structure like this:所以,为此,我创建了一个这样的结构:

class MyStructure{
   final int id;
   final String text;
   MyStructure({this.id, this.text});
}

Then I created a widget like this:然后我创建了一个这样的小部件:

class MyWidget extends StatelessWidget{
   final MyStructure widgetStructure;
   MyWidget(this.widgetStructure);

   @override
   Widget build(BuildContext context) {
      return Container(
         child: Text(widgetStructure.id, widgetStructure.text);
         ...
      )
   }
}

Then I created a list of structures like this:然后我创建了一个这样的结构列表:

  List<MyStructure> widgetList;
  widgetList= [MyStructure(
     id = 1;
     text = "a text"
  )];

So, now I could create a list of widget just doing something like this:所以,现在我可以创建一个小部件列表,只需执行以下操作:

return new Row(children: widgetList.map((item) => new MyWidget(item)).toList());

And, in theory, it works but I don't know how to use it with the swiper.而且,从理论上讲,它可以工作,但我不知道如何将它与刷卡器一起使用。

In demo, widgetList length is 10. you can see full code below在演示中,widgetList 长度为 10。您可以在下面看到完整的代码

full code完整代码

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new InnerSwiper(),
    );
  }
}

class MyStructure {
  final int id;
  final String text;
  MyStructure({this.id, this.text});
}

class MyWidget extends StatelessWidget {
  final MyStructure widgetStructure;
  MyWidget(this.widgetStructure);

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.green,
        child: Text('${widgetStructure.id} ${widgetStructure.text}')
    );
  }
}

class InnerSwiper extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _InnerSwiperState();
  }
}

class _InnerSwiperState extends State<InnerSwiper> {
  SwiperController controller;

  List<bool> autoplayes;

  List<SwiperController> controllers;
  List<MyStructure> widgetList = [];

  @override
  void initState() {
    controller = new SwiperController();
    autoplayes = new List()
      ..length = 10
      ..fillRange(0, 10, false);
    controllers = new List()
      ..length = 10
      ..fillRange(0, 10, new SwiperController());

    for(int i=0;i < 10; i++) {
      widgetList.add(MyStructure(id:i,text: ' this is index ${i}'));
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: new Scaffold(
        body: new Swiper(
          loop: false,
          itemCount: widgetList.length,
          controller: controller,
          pagination: new SwiperPagination(),
          itemBuilder: (BuildContext context, int index) {
            return new Column(
              children: <Widget>[
                new SizedBox(
                  child: new Swiper(
                    controller: controllers[index],
                    pagination: new SwiperPagination(),
                    itemCount: widgetList.length,
                    itemBuilder: (BuildContext context, int index) {
                      return MyWidget(widgetList[index]);
                    },
                    autoplay: autoplayes[index],
                  ),
                  height: 300.0,
                ),
                new RaisedButton(
                  onPressed: () {
                    setState(() {
                      autoplayes[index] = true;
                    });
                  },
                  child: new Text("Start autoplay"),
                ),
                new RaisedButton(
                  onPressed: () {
                    setState(() {
                      autoplayes[index] = false;
                    });
                  },
                  child: new Text("End autoplay"),
                ),
                new Text("is autoplay: ${autoplayes[index]}")
              ],
            );
          },
        ),
      ),
    );
  }
}

在此处输入图像描述

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class NewSetPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final List<Widget> steps = [
      _step0(),
      _step1(),
      _step2(),
    ];
    return Scaffold(
      appBar: AppBar(
        title: Text('New set'),
      ),
      body: SafeArea(
        top: true,
        bottom: true,
        child: Swiper(
          itemBuilder: (BuildContext context, int index) {
            return steps[index];
          },
          loop: false,
          itemCount: steps.length,
          control: new SwiperControl(),
        ),
      ),
    );
  }

  Widget _step0() {
    return TextField(
      decoration: InputDecoration(labelText: "Add Elements"),
      onSubmitted: (elem) {
      },
    );
  }

  Widget _step1() {
    return Text("This is the step 1");
  }

  Widget _step2() {
    return Text("This is the step 2");
  }
}

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

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