图像滑块不随图像网格索引移动

问题描述 投票:0回答:1

我在 Clip React 小部件中有一个图像滑块,并且在其底部有一个图像网格视图,例如图库。但是当我滑动图像时,它会更改网格部分中的索引,但是当我点击网格并从那里更改时,它无法更改图片

这是我的代码,有人知道这个问题吗?

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:osticket_app/ticket_details/attachments/attachement_preview.dart';
import 'package:osticket_app/utils/colors.dart';

import '../../utils/responsive_layout.dart';

class MyCustomMobileContent extends StatelessWidget {
  const MyCustomMobileContent({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: ResposiveLayout(
        mobileBody: ImageSlider(),
      ),
    );
  }
}

List<String> imgList = [
  'assets/images/attachment.png',
  'assets/images/school.png',
  'assets/images/medic.png',
  'assets/images/attachment.png',
  'assets/images/medic.png',
  'assets/images/school.png',
  'assets/images/attachment.png',
  'assets/images/medic.png',
];

class ImageSlider extends StatefulWidget {
  const ImageSlider({super.key});

  @override
  State<ImageSlider> createState() => _ImageSliderState();
}

class _ImageSliderState extends State<ImageSlider> {
  late List<Widget> imageSliders;
  int _current = 0;
  int selected = 0;
  final CarouselController _controller = CarouselController();
  @override
  Widget build(BuildContext context) {
    imageSliders = imgList
        .map(
          (item) => ClipRRect(
            borderRadius: BorderRadius.circular(
              10.r,
            ),
            child: Stack(
              children: <Widget>[
                Center(
                  child: GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (ctx) => AttachementPreview(
                            image: item,
                          ),
                        ),
                      );
                    },
                    child: Image.asset(
                      fit: BoxFit.fill,
                      item,
                    ),
                  ),
                ),
              ],
            ),
          ),
        )
        .toList();
    return Scaffold(
      appBar: AppBar(
        actions: [
          Padding(
            padding: EdgeInsets.only(right: 10.w),
            child: TextButton(
              onPressed: () {},
              child: Text(
                'Download',
                style: TextStyle(
                    fontSize: 14.sp,
                    color: buttonColor,
                    fontWeight: FontWeight.w500),
              ),
            ),
          ),
        ],
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(
            1.h,
          ),
          child: Divider(
            color: textFieldBg,
            height: 2.h,
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            decoration: BoxDecoration(
                border: Border.all(
              width: 1.w,
              color: textFieldBg,
            )),
            child: CarouselSlider(
              items: imageSliders,
              carouselController: _controller,
              options: CarouselOptions(
                  height: 500.h,
                  viewportFraction: 1,
                  autoPlay: false,
                  // enlargeCenterPage: true,
                  aspectRatio: 2.0,
                  onPageChanged: (index, reason) {
                    setState(() {
                      _current = index;
                    });
                  }),
            ),
          ),
          SizedBox(
            height: 20.h,
          ),
          Text(
            'Medical-claim-cardiologist.png',
            style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w700),
          ),
          SizedBox(
            height: 20.h,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: imgList.asMap().entries.map((entry) {
              return GestureDetector(
                onTap: () => _controller.animateToPage(entry.key),
                child: Container(
                  width: 8.w,
                  height: 8.h,
                  margin: EdgeInsets.symmetric(vertical: 8.h, horizontal: 4.w),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: (Theme.of(context).brightness == Brightness.dark
                            ? backgroundColor
                            : buttonColor)
                        .withOpacity(_current == entry.key ? 0.9 : 0.2),
                  ),
                ),
              );
            }).toList(),
          ),
          SizedBox(
            height: 20.h,
          ),
          SizedBox(
            height: 85.h,
            // width: 350.w,
            child: ListView.builder(
              scrollDirection:
                  Axis.horizontal, // Set the scroll direction to horizontal
              itemCount: imgList.length,
              itemBuilder: (context, index) {
                return Padding(
                  padding:
                      EdgeInsets.symmetric(horizontal: 10.h, vertical: 10.w),
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        _current = index;
                      });
                    },
                    child: Container(
                      padding: EdgeInsets.all(1.w),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(6.r),
                        border: Border.all(
                            width: 1.6.w,
                            color: _current == index
                                ? buttonColor
                                : Colors.transparent),
                      ),
                      child: Image.asset(
                        imgList[index],
                        width: 50.w, // Set the width of the image
                        height: 150.h, // Set the height of the image
                        fit: BoxFit
                            .fitHeight, // You can adjust the fit as needed
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
          // ListView.builder(itemBuilder: itemBuilder)
        ],
      ),
    );
  }
}

正如您所看到的代码,我希望您理解我的问题并尝试尽快解决此问题,提前致谢

flutter dart mobile flutter-dependencies flutter-animation
1个回答
0
投票

改变这个:

 onTap: () => _controller.animateToPage(entry.key),

对此:

onTap:(){
setState((){

 _controller.animateToPage(entry.key),
}}
© www.soinside.com 2019 - 2024. All rights reserved.