如何在底片内部实现滑块。我试过了,但是滑块没有移动。但是如果我将滑块放在新的脚手架中就可以了。帮我。提前致谢。
这是我的代码。
new InkWell(
onTap: () {
// fire_ac_on_1();
},
onDoubleTap:(){
_showModalSheet_ac();
}
onLongPress: () {
selectTime(context);
},
child: new IconButton(
icon: new Image.asset('images/ac.png',
color: ac_on_state == true ? Colors
.green : Colors.redAccent),
onPressed: () {
fire_ac_on();
},
iconSize: 80.0,
),
),
//模态底页 无效_showModalSheet_ac(){
double height = MediaQuery.of(context).size.height;
//print(height);
showModalBottomSheet(context: context, builder: (builder){
return new Container(
height: height - 200.0,
child: new Center(
child: new Column(
children: <Widget>[
new Container(
child: new Slider(
value: slider_value,
onChanged: onchanged),
)
)
],
),
)
);
});
}
onchanged(双值){
setState(() {
slider_value = value;
});
print(slider_value);
}
答案 0 :(得分:4)
bottomSheet不是有状态的,这就是为什么当您在Slider中调用setState时它不会重建的原因。
您必须使用StatefulBuilder包装Slider小部件。
StatefulBuilder(
builder: (context, setState){
return Slider(
value: _value,
onChanged: (selection){
setState(() {
_value = selection;
});
},
);
},
),
答案 1 :(得分:0)
您必须将 BuildContext 传递给 builder 而不是builder。我希望以下示例可以为您提供帮助。
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Bottom modal tutorial',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Bottom modal tutorial'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
mainBottomSheet(BuildContext context){
showModalBottomSheet(
context: context,
builder: (BuildContext context){
return Container(
height: 300.0,
child: Center(child: new Text("Demo Text"))
);
}
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Container(
child: new Center(
child: new Text(
'Body',
),
),
),
floatingActionButton: new FloatingActionButton(
onPressed: () => mainBottomSheet(context),
child: new Icon(Icons.add),
),
);
}
}
答案 2 :(得分:0)
我已经使用StreamBuilder
和RxDart解决了相同的问题:
import 'package:rxdart/rxdart.dart';
final _slider = PublishSubject<double>();
Observable<double> get sliderStream => _slider.stream;
void _settingModalBottomSheet(context){
showModalBottomSheet(
context: context,
builder: (BuildContext bc){
return StreamBuilder(
builder: (BuildContext context, AsyncSnapshot<double> snapshot) {
return Slider(
activeColor: Colors.indigoAccent,
min: 0.0,
max: 15.0,
onChanged: (newRating) {
_slider.sink.add(newRating);
},
value: snapshot.data,
);
},
initialData: 0.0,
stream: sliderStream,
);
}
);
}
在每个onChanged
事件中,您将 发出 新滑块的值(_slider.sink.add(newRating);
)。每次滑块值更改时,都会通知StreamBuilder
,并使用更新后的值(value: snapshot.data
)重建小部件。