pal*_*lak 1 rangeslider flutter bottom-sheet flutter-layout
当我在底部工作表上使用范围滑块时,我被困在这里,范围滑块无法工作。我收到 0 错误,所以我不知道它卡在哪里。当我在构建函数中使用该函数但不在底部工作表中时,该函数工作正常。下面是它的代码-:
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;
});
},
),
),
],
),
),
),
),
],
),
);
});
}
Run Code Online (Sandbox Code Playgroud)
我在传递变量的文本字段点击上调用它。请让我知道任何解决方案。
编辑-:这是我编辑的代码..
_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;
});
},
),
),
],
),
),
),
),
],
),
);
},
);
});
}
Run Code Online (Sandbox Code Playgroud)
您showModalBottomSheet正在调用,底部的工作表是一个StateLessWidget
,因此您无法调用setState或更改其中的状态。为了解决这个问题,我建议用 a 包裹 内部Container,然后您可以在内部调用
并更改值。bottomModalSheetStateFullBuildersetStatebottomModelSheetRanageSlider
用这个替换你的 _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;
});
},
),
),
],
),
),
),
),
],
),
);
},);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1903 次 |
| 最近记录: |