Created
August 18, 2022 12:45
-
-
Save DaisukeNagata/59409c56ae3a3a8ea0f79aad1d574390 to your computer and use it in GitHub Desktop.
carouSel Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({ | |
super.key, | |
}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final ScrollController _c = ScrollController(); | |
final List<int> _itemList = []; | |
final _displayCount = 4; | |
final _itemCount = 30; | |
var _carouSelCount = 0; | |
var _carouSelFlg = true; | |
var _pixels = 0.0; | |
@override | |
void initState() { | |
super.initState(); | |
for (var i = 0; i < _itemCount; i++) { | |
_itemList.add(i); | |
} | |
_c.addListener(() { | |
setState(() { | |
var value = (MediaQuery.of(context).size.width / _displayCount) / 2; | |
_carouSelCheck(value); | |
}); | |
}); | |
} | |
void _carouSelCheck(double value) { | |
if (_carouSelFlg) { | |
if (_c.position.pixels > _pixels) { | |
_carouSelFlg = false; | |
_carouSelCount += 2; | |
} else { | |
_carouSelFlg = false; | |
_carouSelCount -= 2; | |
} | |
_c | |
.animateTo((_carouSelCount * value * 2), | |
duration: const Duration(milliseconds: 500), curve: Curves.easeIn) | |
.then((value) => _carouSelFlg = true); | |
} | |
_pixels = _c.position.pixels; | |
} | |
@override | |
Widget build(BuildContext context) { | |
var value = MediaQuery.of(context).size.width / _displayCount; | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('ListView.builder'), | |
centerTitle: true, | |
), | |
body: Center( | |
child: SizedBox( | |
height: 200, | |
child: GestureDetector( | |
child: ListView.builder( | |
controller: _c, | |
scrollDirection: Axis.horizontal, | |
itemCount: _itemList.length, | |
itemBuilder: (BuildContext context, int index) { | |
return Container( | |
alignment: Alignment.center, | |
margin: | |
EdgeInsets.only(left: index == 0 ? (value) * 1.5 : value), | |
height: value, | |
width: value, | |
color: index.isOdd ? Colors.white : Colors.red, | |
child: TextButton.icon( | |
onPressed: () {}, | |
icon: const Icon(Icons.add), | |
label: Text('${_itemList[index]}'), | |
), | |
); | |
}, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
exam.mov