从 Flutter 中的另一个类访问更改后的 Slider 值

问题描述 投票:0回答:2

我有一个类,其中有一个 slider.adaptive 以及它的整个布局。在我的主类中,我调用该滑块类并声明布局的值(标题等)。我遇到的问题是滑块本身的值无法访问并保存在调用滑块类的类中。 我仍然是 Flutter 的初学者,所以我喜欢尽可能简单的解释,这样我就能理解它们。

我目前拥有的代码:

class CustomSlider extends StatefulWidget {
  final double value;
  final String title;
  final String good;
  final String bad;

  CustomSlider({
    required this.value,
    required this.title,
    required this.good,
    required this.bad,
  });

  @override
  _CustomSlider createState() {
    return _CustomSlider(value: value, title: title, good: good, bad: bad);
  }
}

class _CustomSlider extends State<CustomSlider> {
  double value;
  String title = "";
  double height = 15.0;
  double width = 10.0;
  String good;
  String bad;

  _CustomSlider({
    required this.value,
    required this.title,
    required this.good,
    required this.bad,
  });

  //double value = 0.0;
  @override
  Widget build(BuildContext context) {
    return Container(

//more layout here in-between

                   Expanded(
                      child: Slider.adaptive(
                        value: value,
                        min: 0,
                        max: 10,
                        divisions: 10,
                        activeColor: Colors.cyanAccent,
                        onChanged: (double changedValue) {
                          setState((){
                              value = changedValue;
                          });
                          print(value);
                        },

                        // semanticFormatterCallback: (double endValue){
                        //   return '$value';
                        // },
                        label: value.toString(),
                      ),
                    ),

调用 Customslider 的类:

class RecordSymptoms extends StatefulWidget {
  @override
  State createState() => new _RecordSymptoms();
}

class _RecordSymptoms extends State<RecordSymptoms> {
  final double height = 20;
  double symptomTotal = 0;
  var generalWellbeing1;
  double generalWellbeing = 0;
  double cramps = 0;
  double flatulence = 0;
  double bowel = 0;

  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: Colors.teal[100],
        endDrawer: menu(),
        appBar: AppBar(
          title: Text(
            'Symptome erfassen',
            style: TextStyle(
              color: Colors.black,
            ),
          ),
          backgroundColor: Colors.cyanAccent,
        ),
        body: Stack(
          children: <Widget>[
            Positioned.fill(
              child: Image(
                image: AssetImage('assets/Inner_Peace.png'),
                fit: BoxFit.fill,
              ),
            ),
            Column(
              children: <Widget>[
                CustomSlider(
                    value: generalWellbeing,
                    title: 'Allgemeines Wohlbefinden',
                    good: "Gut",
                    bad: "Schlecht"),
                CustomSlider(
                    value: cramps,
                    title: 'Krämpfe',
                    good: "Keine",
                    bad: "Extrem"),
                CustomSlider(
                    value: flatulence,
                    title: 'Blähungen',
                    good: "Keine",
                    bad: "Extrem"),
                CustomSlider(
                    value: bowel,
                    title: 'Stuhlgang',
                    good: "Fest",
                    bad: "Flüssig"),
                Spacer(),
                Container(
                  child: Row(
                    children: <Widget>[
                      Flexible(
                        flex: 20,
                        child: CustomButton(
                          text: 'Keine Symptome',
                          onClick: () {
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                builder: (context) => RecordedMeals(),
                              ),
                            );
                          },
                        ),
                      ),
                      Flexible(
                        flex: 20,
                        child: CustomButton(
                          text: 'Symptome speichern',
                          onClick: () async {
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                builder: (context) => RecordedMeals(),
                              ),
                            );
                            print(generalWellbeing);
                            print(bowel.toString());
                            print(cramps.toString());
                            print(flatulence.toString());
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(height: height),
              ],
            ),
          ],
        ),
      );

我尝试了

semanticFormatterCallback
,但我不知道如何将其传递给其他班级。我也尝试过将类更改为函数,但随后出现了所有四个滑块的滑块一起移动的问题。

print(flatulence.toString());
和其他都打印出0.0,当它无法从其他类获取值时这是有意义的,因为我用0初始化了它们。

flutter callback slider
2个回答
0
投票

我不知道为什么当时没有人回答你的问题,但这是合法的方法......

1 - 在构造函数中创建回调,将其命名为“onChanged”,因为它只会从滑块自己的 onChanged 属性传递值。请注意,这与为您提供的任何滑块的默认 onChanged 属性不同。这将在默认的 onChanged 中用作您的 onChanged (您可以像我所说的那样将其命名为您喜欢的名称)。

class SliderWidget extends StatefulWidget {
  final double sliderHeight;
  final int min;
  final int max;
  final fullWidth;
  final ValueChanged<int> onChanged; // HERE IS THE IMPORTANT PART -> DO THIS
  // late final int value;

  SliderWidget({
    this.sliderHeight = 60,
    this.max = 5,
    this.min = 0,
    this.fullWidth = false,
    required this.onChanged,  // HERE IS THE IMPORTANT PART -> DO THIS
    // required this.value,
  });

这个 sliderWidget 的效果介于两者之间:

 Slider(
              value: indexTop.toDouble(),
              min: min,
              max: max,
              divisions: divisions,
              // label: labels[indexTop],
              onChanged: (value) => // THIS IS DEFAULT PROPERTY!
                  setState(() {
                    this.indexTop = value.toInt();
                    widget.onChanged.call(value.toInt()); // THIS IS YOUR CALLBACK!
                    // widget.value = value.toInt();
                  }),
            ),

2-现在只需在任何其他小部件类中创建此小部件的实例,每次对该实例进行更改时您的回调都会侦听...

      new SliderWidget(onChanged: (int value) { 
      print(value.toInt();
}
,),

如果仍然难以执行,请写信给我,我会向您和任何想要实现相同结果的人解释......


0
投票

这很好,但是如果我想在类之外控制滑块值怎么办?

© www.soinside.com 2019 - 2024. All rights reserved.