Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在不同页面上使用未按索引的旋转图像_Flutter_Dart_Indexing_Carousel - Fatal编程技术网

Flutter 在不同页面上使用未按索引的旋转图像

Flutter 在不同页面上使用未按索引的旋转图像,flutter,dart,indexing,carousel,Flutter,Dart,Indexing,Carousel,我是个新手。我需要有关旋转木马图像的帮助。我有使用carousel_pro软件包的旋转木马图像。当使用手势检测器按下时,我可以在新页面上打开图像。但我想在新的页面上打开相同的旋转木马图像,但首先使用onPressed索引。我是说,我有5张照片。当我按下第三个图像时,它必须在新的旋转木马上打开,该图像不是第一个。我希望我说清楚了。下面是一张图片的一页。我需要旋转木马与启动索引的按下的图像在旋转木马的图像。提前谢谢 import 'package:carousel_slider/carousel_s

我是个新手。我需要有关旋转木马图像的帮助。我有使用carousel_pro软件包的旋转木马图像。当使用手势检测器按下时,我可以在新页面上打开图像。但我想在新的页面上打开相同的旋转木马图像,但首先使用onPressed索引。我是说,我有5张照片。当我按下第三个图像时,它必须在新的旋转木马上打开,该图像不是第一个。我希望我说清楚了。下面是一张图片的一页。我需要旋转木马与启动索引的按下的图像在旋转木马的图像。提前谢谢

import 'package:carousel_slider/carousel_slider.dart';
import './image_screen.dart';

void main() => runApp(MaterialApp(home: Demo()));

class Demo extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<Demo> {
  @override
  Widget build(BuildContext context) {
    Widget image_carousel = new Container(
        height: 345.0,
        child: CarouselSlider(
          height: 400.0,
          items: [
            'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
            'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg',
            'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
            'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg'
          ].map((i) {
            return Builder(
              builder: (BuildContext context) {
                return Container(
                    width: MediaQuery.of(context).size.width,
                    margin: EdgeInsets.symmetric(horizontal: 5.0),
                    decoration: BoxDecoration(color: Colors.amber),
                    child: GestureDetector(
                        child: Image.network(i, fit: BoxFit.fill),
                        onTap: () {
                          Navigator.push<Widget>(
                            context,
                            MaterialPageRoute(
                              builder: (context) => ImageScreen(i),
                            ),
                          );
                        }));
              },
            );
          }).toList(),
        ));

    return Scaffold(
      body: new Column(
        children: <Widget>[
          image_carousel,
        ],
      ),
    );
  }
}
import'package:carousel_slider/carousel_slider.dart';
导入“/image_screen.dart”;
void main()=>runApp(MaterialApp(home:Demo());
类Demo扩展StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
Widget image_carousel=新容器(
高度:345.0,
儿童:旋转滑翔机(
高度:400.0,
项目:[
'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg',
'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg'
].地图((i){
返回生成器(
生成器:(BuildContext上下文){
返回容器(
宽度:MediaQuery.of(context).size.width,
边缘:边缘组。对称(水平:5.0),
装饰:盒子装饰(颜色:颜色。琥珀色),
儿童:手势检测器(
子项:Image.network(i,fit:BoxFit.fill),
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>ImageScreen(i),
),
);
}));
},
);
}).toList(),
));
返回脚手架(
正文:新栏目(
儿童:[
图像传送带,
],
),
);
}
}

编辑:我为示例添加了上述代码。这不是我需要的。我需要这样的smth:第1页有5个图像的旋转木马。当我按下第三张图像时,在第2页上,相同的图像旋转木马打开,其中包含按下的第三张图像的索引。我的意思是在第2页,旋转木马必须从第3张图片开始。希望这次我能说清楚。

这是一个代码示例,可能会对您有所帮助。
主屏幕

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test"),
      ),
      body: Container(
        height: 345.0,
        child: CarouselSlider(
          options: CarouselOptions(
            height: 400,
          ),
          items: [
            'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
            'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg',
            'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
            'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg'
          ].map(
            (i) {
              return Container(
                width: MediaQuery.of(context).size.width,
                margin: EdgeInsets.symmetric(horizontal: 5.0),
                decoration: BoxDecoration(color: Colors.amber),
                child: GestureDetector(
                  child: Image.network(i, fit: BoxFit.fill),
                  onTap: () {
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => Screen2(
                          image: i,
                        ),
                      ),
                    );
                  },
                ),
              );
            },
          ).toList(),
        ),
      ),
    );
  }
}
屏幕2:带有旋转木马和appBar的屏幕,可返回到第一个屏幕

class Screen2 extends StatelessWidget {
  final List<String> images;
  final String selectedImages;
  Screen2({this.images, this.selectedImages});
  @override
  Widget build(BuildContext context) {
    images.removeWhere((i) => i == selectedImages);
    images.insert(0, selectedImages);
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        height: double.maxFinite,
        width: double.maxFinite,
        child: CarouselSlider(
          options: CarouselOptions(
            viewportFraction: 1.0,
            height: double.maxFinite,
          ),
          items: [
            ...images.map(
              (image) => Image.network(
                image,
                fit: BoxFit.fill,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class Screen2扩展了无状态小部件{
最后列出图像;
最终字符串选择图像;
屏幕2({this.images,this.selectedImages});
@凌驾
小部件构建(构建上下文){
images.removehere((i)=>i==selectedImages);
图像。插入(0,选择图像);
返回脚手架(
appBar:appBar(),
主体:容器(
高度:double.maxFinite,
宽度:double.maxFinite,
儿童:旋转滑翔机(
选项:旋转木马(
视口分数:1.0,
高度:double.maxFinite,
),
项目:[
…images.map(
(图像)=>image.network(
形象,,
fit:BoxFit.fill,
),
),
],
),
),
);
}
}

结果视频:

这是一个代码示例,可能会对您有所帮助。
主屏幕

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test"),
      ),
      body: Container(
        height: 345.0,
        child: CarouselSlider(
          options: CarouselOptions(
            height: 400,
          ),
          items: [
            'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
            'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg',
            'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
            'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg'
          ].map(
            (i) {
              return Container(
                width: MediaQuery.of(context).size.width,
                margin: EdgeInsets.symmetric(horizontal: 5.0),
                decoration: BoxDecoration(color: Colors.amber),
                child: GestureDetector(
                  child: Image.network(i, fit: BoxFit.fill),
                  onTap: () {
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => Screen2(
                          image: i,
                        ),
                      ),
                    );
                  },
                ),
              );
            },
          ).toList(),
        ),
      ),
    );
  }
}
屏幕2:带有旋转木马和appBar的屏幕,可返回到第一个屏幕

class Screen2 extends StatelessWidget {
  final List<String> images;
  final String selectedImages;
  Screen2({this.images, this.selectedImages});
  @override
  Widget build(BuildContext context) {
    images.removeWhere((i) => i == selectedImages);
    images.insert(0, selectedImages);
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        height: double.maxFinite,
        width: double.maxFinite,
        child: CarouselSlider(
          options: CarouselOptions(
            viewportFraction: 1.0,
            height: double.maxFinite,
          ),
          items: [
            ...images.map(
              (image) => Image.network(
                image,
                fit: BoxFit.fill,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class Screen2扩展了无状态小部件{
最后列出图像;
最终字符串选择图像;
屏幕2({this.images,this.selectedImages});
@凌驾
小部件构建(构建上下文){
images.removehere((i)=>i==selectedImages);
图像。插入(0,选择图像);
返回脚手架(
appBar:appBar(),
主体:容器(
高度:double.maxFinite,
宽度:double.maxFinite,
儿童:旋转滑翔机(
选项:旋转木马(
视口分数:1.0,
高度:double.maxFinite,
),
项目:[
…images.map(
(图像)=>image.network(
形象,,
fit:BoxFit.fill,
),
),
],
),
),
);
}
}

结果视频:

回答版本2.0

请看,UI元素工作正常,需要做的是找到一种方法,首先设置项目,然后在之后推送所有元素

在这里,我们需要了解什么是。请仔细阅读,以理解概念

算法

1. Select index of the image urls
2. If the item which is selected is first, then normal list would be passed
3. If not, then we get firstElements which is a sublist of (0, index), index not included [for example => If we select 3 from 1,2,3,4 => firstElements = [1,2]]
4. We maintain the lastElements, which is a sublist of (index, list.length), where list.length is ignore. [for example => If we select 3 from 1,2,3,4 => lastElements= [3,4]]
4. Finally we make a final list by adding lastElements + firstElements = [3,4,1,2]
这就是我们想要实现的,不是吗

代码实现

  // suppose we have an array of Ints
  List<int> data = [1,2,3,4];
  
  // to keep a pointer, which one is selected
  // here we selected 3, so index will have 2 as it's value, 
  // which is the index of 3
  int index = data.indexOf(3);
  
  // now let say, we want to push 3 to the top, keeping 
  // 4, after it, and 1,2 after 4 in the same format 
  // similar to what you want to do with the selected image
  // CHECKS FOR FIRST ELEMENT IS SELECTED OR NOT, WHICH HAS INDEX 0
  if(index != 0){
    // get the sublist from 0 to index-1
    var firstElements = data.sublist(0, index);
    // get the sublist from index, to last
    var restElements = data.sublist(index, data.length);
    
    // restElements should be pushed to top, since we have want the 
    // to be there, then comes firstElements
    print(restElements+firstElements);
  }else{
    // since the first item is selected
    print(data);
  } 
  
  print(items);

  // OUTPUT => [3, 4, 1, 2]
新建页面

class _MyHomePageState extends State<MyHomePage> {
  // In the list, I have used all the images as different to show you the difference
  // in a clear way
  List<String> _imageUrls = [
    'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg',
    'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg',
    'https://static.toiimg.com/thumb/msid-54559212,width-748,height-499,resizemode=4,imgsize-307081/Bangalore.jpg',
    'https://images.unsplash.com/photo-1535332371349-a5d229f49cb5?ixlib=rb-1.2.1&w=1000&q=80'
  ];
  
  // This is the widget which is responsible for creating 
  // list of images as a slider
  Widget get myWidget{
    List<Widget> _widgets = [SizedBox(width: 20.0)];
    
    for(int i=0; i<_imageUrls.length; i++){
      _widgets.add(
        GestureDetector(
          onTap: (){
            Navigator.of(context).push(
              // Our new page, takes ImageUrls list, and the selected index
             // top perform an operation
              MaterialPageRoute(
                builder: (context) => NewPage(imgUrls: _imageUrls, index: i)
              )
            );
          },
          child: Container(
            width: 300.0,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15.0),
              image: DecorationImage(
                fit: BoxFit.cover,
                image: NetworkImage(_imageUrls[i])
              )
            )
          )
        )
      );
      
      _widgets.add(SizedBox(width: 20.0));
    }
    
    return Container(
      height: 400.0,
      child: ListView(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        children: _widgets
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Center(
          child: this.myWidget
        )
      )
    );
  }
}
class _NewPageState extends State<NewPage> {
  // This will keep a copy of all the items coming from immutable imageUrls
  // We will be doing operation in this list only
  // it has to be initialized as an empty array
  List<String> _newImageUrls = [];
  
  @override
  void initState(){
    super.initState();
    
    // same algo which is explained above
    if(widget.index != 0){
      // get the sublist from 0 to index-1
      var firstElements = widget.imgUrls.sublist(0, widget.index);
      // get the sublist from index, to last
      var restElements = widget.imgUrls.sublist(widget.index, widget.imgUrls.length);

      setState(() => _newImageUrls = restElements + firstElements);
    }else{
      // since the first item is selected
      // no _newImageUrls = widget.imgUrls cos, machine will
      // understand that both the items are same, so if one changes,
      // that means another has to change. So strict no-no to that
      widget.imgUrls.forEach((item){
        _newImageUrls.add(item);
      });
    }
  }
  
  // now this is same as our HomePage 
  // just a height change of the images and we're using our new 
  // list, not the passed one, since, it has changed data now
  Widget get myWidget{
    List<Widget> _widgets = [SizedBox(width: 20.0)];
    
    for(int i=0; i<_newImageUrls.length; i++){
      _widgets.add(
        Container(
          width: 300.0,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15.0),
            image: DecorationImage(
              fit: BoxFit.cover,
              image: NetworkImage(_newImageUrls[i])
            )
          )
        )
      );
      
      _widgets.add(SizedBox(width: 20.0));
    }
    
    return Container(
      height: 500.0,
      child: ListView(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        children: _widgets
      )
    );
  }
  
  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
        title: Text('New Page'),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Center(
          child: this.myWidget
        )
      )
    );
  }
}
class\u NewPageState扩展状态{
//这将保留来自不可变ImageURL的所有项目的副本
//我们将仅在此列表中执行操作
//它必须初始化为空数组
列表_newImageUrls=[];
@凌驾
void initState(){
super.initState();
//与上面解释的算法相同
如果(widget.index!=0){
//获取从0到索引1的子列表
var firstElements=widget.imgUrls.sublist(0,widget.index);
//从索引中获取子列表,直到最后一个
var restElements=widget.imgUrls.sublist(widget.index,widget.imgUrls.length);
setState(()=>\u newImageUrls=restElements+firstElements);
}否则{
//因为选择了第一项
//否_newImageUrls=widget.imgUrls cos,机器将
//了解这两个项目