底部的范围滑块 sheet 无法正常工作
Range slider in bottom sheet not working in flutter
当我在底部使用它时,我被困在这里,我的范围滑块不起作用 sheet。
我得到 0 个错误,所以我不知道它卡在哪里。
当我在构建函数中使用它但在底部 sheet 中不使用时,此函数工作正常。
下面是它的代码-:
RangeValues Myheight = const RangeValues(150, 180);
@override
Widget build(BuildContext context) {
return Scaffold(
body: InkWell(
child: TextField(
controller: interest,
decoration: InputDecoration(
enabled: false,
suffixIcon: Padding(
padding: EdgeInsets.only(top: 15),
// add padding to adjust icon
child: Icon(Icons.add),
),
prefixIcon: Padding(
padding: EdgeInsets.only(top: 15),
// add padding to adjust icon
child: Icon(Icons.favorite_border_rounded),
),
),
),
onTap: () {
_bottomSheet(context, Myheight);
},
),
);
}
_bottomSheet(context, myheight) {
showModalBottomSheet(
context: context,
builder: (BuildContext c) {
return Container(
height: 250,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
fit: FlexFit.loose,
child: Container(
height: 180,
child: ReusableCard(
colors: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HEIGHT',
style: kLabelStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'${myheight.start.round().toString()} - ${myheight.end.round().toString()}',
style: kNumberStyle,
),
Text(
'cm',
style: kLabelStyle,
),
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.pink,
inactiveTrackColor: Color(0xFF8D8E98),
thumbColor: Color(0xFFEB1555),
thumbShape:
RoundSliderThumbShape(enabledThumbRadius: 15),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 30.0),
overlayColor: Color(0x29EB1555),
showValueIndicator: ShowValueIndicator.always,
),
child: RangeSlider(
values: myheight,
min: 100,
max: 220,
labels: RangeLabels('${myheight.start.round()}', '${myheight.end.round()}'),
// divisions: 10,
onChanged: (RangeValues newValue) {
setState(() {
myheight = newValue;
});
},
),
),
],
),
),
),
),
],
),
);
});
}
我在传递变量的文本字段点击上调用它。
请让我知道任何解决方案。
下面分别是工作范围滑块和非工作范围滑块的图像。 -:
下面是代码编辑的图片-:
编辑-:这是我编辑的代码..
_bottomHeightSheet(context) {
RangeValues Myheight = const RangeValues(150, 180);
showModalBottomSheet(
context: context,
builder: (BuildContext c) {
return StatefulBuilder(
builder: (BuildContext context,
void Function(void function()) setState){ // getting error in this line
return Container(
height: 250,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
fit: FlexFit.loose,
child: Container(
height: 180,
child: ReusableCard(
colors: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HEIGHT',
style: kLabelStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'${Myheight.start.round().toString()} - ${Myheight.end.round().toString()}',
style: kNumberStyle,
),
Text(
'cm',
style: kLabelStyle,
),
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.pink,
inactiveTrackColor: Color(0xFF8D8E98),
thumbColor: Color(0xFFEB1555),
thumbShape:
RoundSliderThumbShape(enabledThumbRadius: 15),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 30.0),
overlayColor: Color(0x29EB1555),
showValueIndicator: ShowValueIndicator.always,
),
child: RangeSlider(
values: Myheight,
min: 100,
max: 220,
labels: RangeLabels('${Myheight.start.round()}', '${Myheight.end.round()}'),
// divisions: 10,
onChanged: (RangeValues newValue) {
setState(() {
Myheight = newValue;
});
},
),
),
],
),
),
),
),
],
),
);
},
);
});
}
你正在调用的showModalBottomSheet
,下面sheet是一个StateLessWidget
因此你不能调用 setState
或改变其中的状态。要解决此问题,我建议用 StateFullBuilder
将 Container
包装在 bottomModalSheet
内,然后您可以在 bottomModelSheet
内调用 setState
并更改 RanageSlider
值。
用这个替换你的 _bottomSheet 函数
showModalBottomSheet(
context: context,
builder: (BuildContext c) {
return StatefulBuilder(builder: (BuildContext context, void Function(void Function()) setState) {
return Container(
height: 250,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
fit: FlexFit.loose,
child: Container(
height: 180,
child: ReusableCard(
colors: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HEIGHT',
style: kLabelStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'${myheight.start.round().toString()} - ${myheight.end.round().toString()}',
style: kNumberStyle,
),
Text(
'cm',
style: kLabelStyle,
),
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.pink,
inactiveTrackColor: Color(0xFF8D8E98),
thumbColor: Color(0xFFEB1555),
thumbShape:
RoundSliderThumbShape(enabledThumbRadius: 15),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 30.0),
overlayColor: Color(0x29EB1555),
showValueIndicator: ShowValueIndicator.always,
),
child: RangeSlider(
values: myheight,
min: 100,
max: 220,
labels: RangeLabels('${myheight.start.round()}', '${myheight.end.round()}'),
// divisions: 10,
onChanged: (RangeValues newValue) {
setState(() {
myheight = newValue;
});
},
),
),
],
),
),
),
),
],
),
);
},);
});
}
当我在底部使用它时,我被困在这里,我的范围滑块不起作用 sheet。 我得到 0 个错误,所以我不知道它卡在哪里。 当我在构建函数中使用它但在底部 sheet 中不使用时,此函数工作正常。 下面是它的代码-:
RangeValues Myheight = const RangeValues(150, 180);
@override
Widget build(BuildContext context) {
return Scaffold(
body: InkWell(
child: TextField(
controller: interest,
decoration: InputDecoration(
enabled: false,
suffixIcon: Padding(
padding: EdgeInsets.only(top: 15),
// add padding to adjust icon
child: Icon(Icons.add),
),
prefixIcon: Padding(
padding: EdgeInsets.only(top: 15),
// add padding to adjust icon
child: Icon(Icons.favorite_border_rounded),
),
),
),
onTap: () {
_bottomSheet(context, Myheight);
},
),
);
}
_bottomSheet(context, myheight) {
showModalBottomSheet(
context: context,
builder: (BuildContext c) {
return Container(
height: 250,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
fit: FlexFit.loose,
child: Container(
height: 180,
child: ReusableCard(
colors: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HEIGHT',
style: kLabelStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'${myheight.start.round().toString()} - ${myheight.end.round().toString()}',
style: kNumberStyle,
),
Text(
'cm',
style: kLabelStyle,
),
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.pink,
inactiveTrackColor: Color(0xFF8D8E98),
thumbColor: Color(0xFFEB1555),
thumbShape:
RoundSliderThumbShape(enabledThumbRadius: 15),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 30.0),
overlayColor: Color(0x29EB1555),
showValueIndicator: ShowValueIndicator.always,
),
child: RangeSlider(
values: myheight,
min: 100,
max: 220,
labels: RangeLabels('${myheight.start.round()}', '${myheight.end.round()}'),
// divisions: 10,
onChanged: (RangeValues newValue) {
setState(() {
myheight = newValue;
});
},
),
),
],
),
),
),
),
],
),
);
});
}
我在传递变量的文本字段点击上调用它。 请让我知道任何解决方案。
下面分别是工作范围滑块和非工作范围滑块的图像。 -:
下面是代码编辑的图片-:
编辑-:这是我编辑的代码..
_bottomHeightSheet(context) {
RangeValues Myheight = const RangeValues(150, 180);
showModalBottomSheet(
context: context,
builder: (BuildContext c) {
return StatefulBuilder(
builder: (BuildContext context,
void Function(void function()) setState){ // getting error in this line
return Container(
height: 250,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
fit: FlexFit.loose,
child: Container(
height: 180,
child: ReusableCard(
colors: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HEIGHT',
style: kLabelStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'${Myheight.start.round().toString()} - ${Myheight.end.round().toString()}',
style: kNumberStyle,
),
Text(
'cm',
style: kLabelStyle,
),
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.pink,
inactiveTrackColor: Color(0xFF8D8E98),
thumbColor: Color(0xFFEB1555),
thumbShape:
RoundSliderThumbShape(enabledThumbRadius: 15),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 30.0),
overlayColor: Color(0x29EB1555),
showValueIndicator: ShowValueIndicator.always,
),
child: RangeSlider(
values: Myheight,
min: 100,
max: 220,
labels: RangeLabels('${Myheight.start.round()}', '${Myheight.end.round()}'),
// divisions: 10,
onChanged: (RangeValues newValue) {
setState(() {
Myheight = newValue;
});
},
),
),
],
),
),
),
),
],
),
);
},
);
});
}
你正在调用的showModalBottomSheet
,下面sheet是一个StateLessWidget
因此你不能调用 setState
或改变其中的状态。要解决此问题,我建议用 StateFullBuilder
将 Container
包装在 bottomModalSheet
内,然后您可以在 bottomModelSheet
内调用 setState
并更改 RanageSlider
值。
用这个替换你的 _bottomSheet 函数
showModalBottomSheet(
context: context,
builder: (BuildContext c) {
return StatefulBuilder(builder: (BuildContext context, void Function(void Function()) setState) {
return Container(
height: 250,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
fit: FlexFit.loose,
child: Container(
height: 180,
child: ReusableCard(
colors: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HEIGHT',
style: kLabelStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'${myheight.start.round().toString()} - ${myheight.end.round().toString()}',
style: kNumberStyle,
),
Text(
'cm',
style: kLabelStyle,
),
],
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.pink,
inactiveTrackColor: Color(0xFF8D8E98),
thumbColor: Color(0xFFEB1555),
thumbShape:
RoundSliderThumbShape(enabledThumbRadius: 15),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 30.0),
overlayColor: Color(0x29EB1555),
showValueIndicator: ShowValueIndicator.always,
),
child: RangeSlider(
values: myheight,
min: 100,
max: 220,
labels: RangeLabels('${myheight.start.round()}', '${myheight.end.round()}'),
// divisions: 10,
onChanged: (RangeValues newValue) {
setState(() {
myheight = newValue;
});
},
),
),
],
),
),
),
),
],
),
);
},);
});
}