Flutter 颤振转盘滑块指示器颜色未更改
我有一个简单的转盘滑块,里面有指示点。问题是我的圆点颜色一起改变。平均值当_dotindicatoR变为0时,所有圆都变为蓝色,当它变为1、2或3时变为灰色。意味着所有的颜色一起改变 我的代码Flutter 颤振转盘滑块指示器颜色未更改,flutter,dart,Flutter,Dart,我有一个简单的转盘滑块,里面有指示点。问题是我的圆点颜色一起改变。平均值当_dotindicatoR变为0时,所有圆都变为蓝色,当它变为1、2或3时变为灰色。意味着所有的颜色一起改变 我的代码 final List<String> imgList = [ 'images/diana-polekhina-F68HtpYHu_w-unsplash.jpg', 'images/diana-polekhina-F68HtpYHu_w-unsplash.jpg', 'images/
final List<String> imgList = [
'images/diana-polekhina-F68HtpYHu_w-unsplash.jpg',
'images/diana-polekhina-F68HtpYHu_w-unsplash.jpg',
'images/diana-polekhina-F68HtpYHu_w-unsplash.jpg',
'images/diana-polekhina-F68HtpYHu_w-unsplash.jpg',
];
final List<Widget> imageSliders = imgList
.map((item) => Container(
child: Container(
margin: EdgeInsets.all(5.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
child: Stack(
children: <Widget>[
Image.asset(
item,
fit: BoxFit.cover,
),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.red, Colors.blue],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
),
),
],
)),
),
))
.toList();
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _dotindicatoR = 0;
String notLength = "0";
Timer timer;
@override
void initState() {
super.initState();
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
double xOffset = 0;
double yOffset = 0;
int pageIndex = 0;
double scaleFactor = 1;
var rating = 3.0;
bool isDrawerOpen = false;
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
print(width);
return AnimatedContainer(
transform: Matrix4.translationValues(xOffset, yOffset, 0)
..scale(scaleFactor)
..rotateY(isDrawerOpen ? -0.5 : 0),
duration: Duration(milliseconds: 370),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(isDrawerOpen ? 40 : 0.0)),
child: GestureDetector(
onTap: () {
isDrawerOpen
? setState(() {
xOffset = 0;
yOffset = 0;
scaleFactor = 1;
isDrawerOpen = false;
})
: print('sada');
},
child: Container(
child: ClipRRect(
borderRadius: isDrawerOpen
? BorderRadius.circular(40.0)
: BorderRadius.circular(0),
child: Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Container(
child: Column(children: [
CarouselSlider(
options: CarouselOptions(
autoPlay: true,
aspectRatio: 1.85,
enlargeCenterPage: true,
enlargeStrategy: CenterPageEnlargeStrategy.height,
onPageChanged: (index, reason) {
print('index ${index}');
setState(() {
_dotindicatoR = index;
print('_current ${_dotindicatoR}');
});
}),
items: imageSliders,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: imgList.map((url) {
int index = imgList.indexOf(url);
return Container(
width: 8.0,
height: 8.0,
margin: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _dotindicatoR == index
? Color(0xff01a8dd)
: Color.fromRGBO(0, 0, 0, 0.4),
),
);
}).toList(),
),
])),
),
),
),
),
));
}
}
最终列表imgList=[
“images/diana-polekhina-F68HtpYHu_w-unsplash.jpg”,
“images/diana-polekhina-F68HtpYHu_w-unsplash.jpg”,
“images/diana-polekhina-F68HtpYHu_w-unsplash.jpg”,
“images/diana-polekhina-F68HtpYHu_w-unsplash.jpg”,
];
最终列表imageSliders=imgList
.map((项)=>容器(
子:容器(
边距:所有边缘集(5.0),
孩子:ClipRRect(
borderRadius:borderRadius.all(半径.圆形(5.0)),
子:堆栈(
儿童:[
影像资产(
项目,,
适合:BoxFit.cover,
),
定位(
底部:0.0,
左:0.0,
右:0.0,
子:容器(
装饰:盒子装饰(
梯度:线性梯度(
颜色:[颜色。红色,颜色。蓝色],
开始:对齐.bottomCenter,
结束:对齐。上止点,
),
),
),
),
],
)),
),
))
.toList();
类主屏幕扩展StatefulWidget{
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
int _dotindicatoR=0;
字符串notLength=“0”;
定时器;
@凌驾
void initState(){
super.initState();
}
@凌驾
无效处置(){
计时器?.cancel();
super.dispose();
}
双xOffset=0;
双yOffset=0;
int pageIndex=0;
双尺度因子=1;
风险值评级=3.0;
bool isDrawerOpen=false;
@凌驾
小部件构建(构建上下文){
double width=MediaQuery.of(context).size.width;
double height=MediaQuery.of(context).size.height;
打印(宽度);
返回动画容器(
转换:Matrix4.translationValues(xOffset,yOffset,0)
…比例(比例因子)
…旋转(isDrawerOpen?-0.5:0),
持续时间:持续时间(毫秒:370),
装饰:盒子装饰(
颜色:颜色。灰色[200],
边界半径:边界半径。圆形(isDrawerOpen?40:0.0)),
儿童:手势检测器(
onTap:(){
isDrawerOpen
?设置状态(){
xOffset=0;
yOffset=0;
scaleFactor=1;
isDrawerOpen=false;
})
:打印('sada');
},
子:容器(
孩子:ClipRRect(
边界半径:isDrawerOpen
?边界半径。圆形(40.0)
:边界半径。圆形(0),
孩子:脚手架(
背景颜色:Colors.white,
正文:SingleChildScrollView(
子:容器(
子项:列(子项:[
旋转滑翔机(
选项:旋转木马(
自动播放:对,
方面:1.85,
放大中心页:正确,
放大策略:CenterPageEnlargeStrategy.height,
onPageChanged:(索引,原因){
打印('index${index}');
设置状态(){
_dotindicatoR=指数;
打印(“U当前${U点指示器}”);
});
}),
项目:图像滑块,
),
划船(
mainAxisAlignment:mainAxisAlignment.center,
子项:imgList.map((url){
int index=imgList.indexOf(url);
返回容器(
宽度:8.0,
身高:8.0,
边距:边缘组。对称(
垂直:10.0,水平:2.0),
装饰:盒子装饰(
形状:BoxShape.circle,
颜色:_dotindicatoR==索引
?颜色(0xff01a8dd)
:颜色。来自RGBO(0,0,0,0.4),
),
);
}).toList(),
),
])),
),
),
),
),
));
}
}
像这样
我想得到的是,当索引相同时,只有一个圆会改变颜色,但这里所有的圆都在改变,问题是在点行中。其中您的doing
int index=imgList.indexOf(url)代码>它不设置单独的值
像这样尝试Listview.builder
SizedBox(
height: 30,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: imgList.length,
itemBuilder: (context, i) {
return Container(
width: 8.0,
height: 8.0,
margin: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _dotindicatoR == i
? Color(0xff01a8dd)
: Color.fromRGBO(0, 0, 0, 0.4),
),
);
}),
),