Untitled

 avatar
unknown
plain_text
a year ago
6.7 kB
6
Indexable
///deal slide looping text advertisement widget
class DealList extends StatefulWidget {
  @override
  _DealListState createState() => _DealListState();
}

class AdvText {
  final String? textAdv;
  final List<String?> images;

  AdvText({this.textAdv, required this.images});
}

class _DealListState extends State<DealList> {
  List<AdvText> dealsWithImages = [
    AdvText(textAdv: 'Deal 1: 50% off on Electronics', images: []),
    AdvText(
        textAdv:
            'Deal 2: Buy one get one free on Clothing Buy one get one free on Clothing',
        images: [
          'http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_brown.png',
        ]),
    AdvText(textAdv: 'Deal 3: Special discounts on Home Decor', images: [
      'http://codeskulptor-demos.commondatastorage.googleapis.com/pang/HfReHl5.jpg',
      'http://commondatastorage.googleapis.com/codeskulptor-demos/riceracer_assets/img/tree_4.png'
    ]),
    AdvText(textAdv: '40 people just booked in the last 24 hours', images: [
      'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/back05.jpg',
      'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/back07.jpg',
      'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/back05.jpg',
    ]),
    AdvText(textAdv: 'Deal 3: 1000% off on Electronics', images: []),
    AdvText(
        textAdv:
        'Deal 2: Buy one get one free on Clothing Buy one get one free on Clothing',
        images: [
          'http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_brown.png',
        ]),
  ];

  int currentIndex = 0; // Start with the first item
  late PageController _pageController;
  late Timer _timer;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(
      viewportFraction: 0.9,
      initialPage: 0,
    );

    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      _pageController.nextPage(
        duration: Duration(milliseconds: 500),
        curve: Curves.easeInOut,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 42.h,
      child: PageView.builder(
        controller: _pageController,
        scrollDirection: Axis.vertical,
        itemBuilder: (context, index) {
          final adjustedIndex = index % dealsWithImages.length;

          return Stack(
            alignment: AlignmentDirectional.centerStart,
            clipBehavior: Clip.hardEdge,
            children: [
              Visibility(
                visible: dealsWithImages[adjustedIndex].images.isNotEmpty,
                child: Container(
                  height: 32,
                  width: 32,
                  margin: EdgeInsets.only(left: 8),
                  decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      border: Border.all(color: BaseColors.white, width: 1)),
                  child: CircleAvatar(
                      radius: 30.0,
                      backgroundColor: BaseColors.white,
                      backgroundImage: dealsWithImages[adjustedIndex]
                              .images
                              .isNotEmpty
                          ? dealsWithImages[adjustedIndex].images[0]?.toNetwork
                          : null),
                ),
              ),
              Visibility(
                visible: dealsWithImages[adjustedIndex].images.length >= 2,
                child: Positioned(
                  left: 28.0,
                  child: Container(
                    height: 32,
                    width: 32,
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        border: Border.all(color: BaseColors.white, width: 1)),
                    child: CircleAvatar(
                      radius: 30.0,
                      backgroundColor: BaseColors.white,
                      backgroundImage:
                          dealsWithImages[adjustedIndex].images.length >= 2
                              ? dealsWithImages[adjustedIndex]
                                  .images[1]
                                  ?.toNetwork
                              : null,
                    ),
                  ),
                ),
              ),
              Visibility(
                visible: dealsWithImages[adjustedIndex].images.length >= 3,
                child: Positioned(
                  left: 48.0,
                  child: Container(
                    height: 32,
                    width: 32,
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        border: Border.all(color: BaseColors.white, width: 1)),
                    child: CircleAvatar(
                      radius: 30.0,
                      backgroundColor: BaseColors.white,
                      backgroundImage:
                          dealsWithImages[adjustedIndex].images.length >= 3
                              ? dealsWithImages[adjustedIndex]
                                  .images[2]
                                  ?.toNetwork
                              : null,
                    ),
                  ),
                ),
              ),
              Positioned(
                  left: dealsWithImages[adjustedIndex].images.isEmpty
                      ? 8.0
                      : dealsWithImages[adjustedIndex].images.length == 1
                          ? dealsWithImages[adjustedIndex].images.length * 50
                          : dealsWithImages[adjustedIndex].images.length == 2
                              ? dealsWithImages[adjustedIndex].images.length *
                                  35
                              : dealsWithImages[adjustedIndex].images.length *
                                  30,
                  child: Container(
                    width: MediaQuery.of(context).size.width * 0.85,
                    child: Text(
                      dealsWithImages[adjustedIndex].textAdv ?? '',
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 13,
                        fontFamily: 'Inter',
                        fontWeight: FontWeight.w500,
                        height: 0,
                      ),
                    ),
                  ))
            ],
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    _timer.cancel();
    _pageController.dispose();
    super.dispose();
  }
}
Editor is loading...
Leave a Comment