Advertisement
Guest User

carousel bug

a guest
Jun 15th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.43 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_carousel_widget/flutter_carousel_widget.dart';
  3.  
  4. void main() {
  5.   runApp(const MyApp());
  6. }
  7.  
  8. class MyApp extends StatelessWidget {
  9.   const MyApp({super.key});
  10.  
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     return const MaterialApp(
  14.       title: 'Demo',
  15.       home: MyHomePage(title: 'Flutter Demo Home Page'),
  16.     );
  17.   }
  18. }
  19.  
  20. class MyHomePage extends StatefulWidget {
  21.   const MyHomePage({super.key, required this.title});
  22.  
  23.   final String title;
  24.  
  25.   @override
  26.   State<MyHomePage> createState() => _MyHomePageState();
  27. }
  28.  
  29. class _MyHomePageState extends State<MyHomePage> {
  30.   CarouselController? controller = CarouselController();
  31.   bool autoplay = false;
  32.   CarouselPageChangedReason? lastReason;
  33.  
  34.   @override
  35.   Widget build(BuildContext context) {
  36.     return Scaffold(
  37.       appBar: AppBar(
  38.         elevation: 10,
  39.         title: Text(widget.title),
  40.         actions: [
  41.           IconButton(
  42.             onPressed: () => setState(() {
  43.               autoplay = !autoplay;
  44.               print('autoplay toggled');
  45.             }),
  46.             icon: Icon(autoplay ? Icons.pause : Icons.play_arrow),
  47.           ),
  48.           IconButton(
  49.             onPressed: () => setState(() {
  50.               controller = controller == null ? CarouselController() : null;
  51.               print('controller toggled');
  52.             }),
  53.             icon: Icon(
  54.               controller == null ? Icons.gamepad_outlined : Icons.gamepad,
  55.             ),
  56.           )
  57.         ],
  58.       ),
  59.       body: Center(
  60.         child: Column(
  61.           mainAxisAlignment: MainAxisAlignment.center,
  62.           children: <Widget>[
  63.             Text(
  64.                 'Last changed reason:\n${lastReason != null ? lastReason!.toString() : 'None'}'),
  65.             FlutterCarousel(
  66.               items: const [
  67.                 Padding(
  68.                   padding: EdgeInsets.all(8.0),
  69.                   child: Placeholder(),
  70.                 )
  71.               ],
  72.               options: CarouselOptions(
  73.                 onPageChanged: (_, reason) => setState(
  74.                   () {
  75.                     lastReason = reason;
  76.                     print(reason);
  77.                   },
  78.                 ),
  79.                 controller: controller,
  80.                 enableInfiniteScroll: true,
  81.                 autoPlay: autoplay,
  82.               ),
  83.             ),
  84.           ],
  85.         ),
  86.       ),
  87.     );
  88.   }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement