Flutter 为什么滑块值返回null?

Flutter 为什么滑块值返回null?,flutter,getter-setter,Flutter,Getter Setter,我正在构建一个包含多个样式滑块的页面,所以我创建了一个单独的SliderWidget类。滑块本身的行为正常,但当我尝试将滑块的值打印到控制台时,它返回null作为flatter:null 我尝试将特定的int值放入我的currentValue变量(在SliderWidget类中)进行测试,但运气不好,所以我很确定问题出在getter本身,但我不知道出了什么问题。我是个新手,所以我很可能做了些傻事 这是滑块对象: SliderWidget moodSlider = new SliderWidget

我正在构建一个包含多个样式滑块的页面,所以我创建了一个单独的SliderWidget类。滑块本身的行为正常,但当我尝试将滑块的值打印到控制台时,它返回null作为
flatter:null

我尝试将特定的int值放入我的currentValue变量(在SliderWidget类中)进行测试,但运气不好,所以我很确定问题出在getter本身,但我不知道出了什么问题。我是个新手,所以我很可能做了些傻事

这是滑块对象:

SliderWidget moodSlider = new SliderWidget(
      sliderHeight: 48,
      min: 0,
      max: 10,
      color1: 0xFF9BC1BC,
      color2: 0xFF9BC1BC);
以下是SliderWidget类:

import 'package:first_tutorial/MoodUpdate/custom_thumb_circle.dart';
import 'package:flutter/material.dart';

class SliderWidget extends StatefulWidget {
  final double sliderHeight;
  final int min;
  final int max;
  final fullWidth;
  final int color1;
  final int color2;
  final int number;

  SliderWidget(
      {this.sliderHeight,
      this.max,
      this.min,
      this.fullWidth = false,
      this.color1,
      this.color2,
      this.number});

  @override
  _SliderWidgetState createState() => _SliderWidgetState();
}

class _SliderWidgetState extends State<SliderWidget> {
  double currentValue = 0;

  int get number => currentValue.round();

  @override
  Widget build(BuildContext context) {
    double paddingFactor = .2;

    if (this.widget.fullWidth) paddingFactor = .3;

    return Container(
      width: this.widget.fullWidth
          ? double.infinity
          : (this.widget.sliderHeight) * 5.5,
      height: (this.widget.sliderHeight),
      decoration: new BoxDecoration(
        borderRadius: new BorderRadius.all(
          Radius.circular((this.widget.sliderHeight * .3)),
        ),
        gradient: new LinearGradient(
            colors: [
              Color(this.widget.color1),
              Color(this.widget.color2),
            ],
            begin: const FractionalOffset(0.0, 0.0),
            end: const FractionalOffset(1.0, 1.00),
            stops: [0.0, 1.0],
            tileMode: TileMode.clamp),
      ),
      child: Padding(
        padding: EdgeInsets.fromLTRB(this.widget.sliderHeight * paddingFactor,
            2, this.widget.sliderHeight * paddingFactor, 2),
        child: Row(
          children: <Widget>[
            Text(
              '${this.widget.min}',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: this.widget.sliderHeight * .3,
                fontWeight: FontWeight.w700,
                color: Colors.white,
              ),
            ),
            SizedBox(
              width: this.widget.sliderHeight * .1,
            ),
            Expanded(
              child: Center(
                child: SliderTheme(
                  data: SliderTheme.of(context).copyWith(
                    activeTrackColor: Colors.white.withOpacity(1),
                    inactiveTrackColor: Colors.white.withOpacity(.5),
                    trackHeight: 4.0,
                    thumbShape: CustomSliderThumbCircle(
                      thumbRadius: this.widget.sliderHeight * .4,
                      darkColor: this.widget.color1,
                      min: this.widget.min,
                      max: this.widget.max,
                    ),
                    overlayColor: Colors.white.withOpacity(.4),
                  ),
                  child: Slider(
                      value: currentValue,
                      onChanged: (value) {
                        setState(() {
                          currentValue = value;
                        });
                      }),
                ),
              ),
            ),
            SizedBox(
              width: this.widget.sliderHeight * .1,
            ),
            Text(
              '${this.widget.max}',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: this.widget.sliderHeight * .3,
                fontWeight: FontWeight.w700,
                color: Colors.white,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
import'package:first_tutorial/MoodUpdate/custom_thumb_circle.dart';
进口“包装:颤振/材料.省道”;
类SliderWidget扩展StatefulWidget{
最后的双滑球;
最终整数分钟;
最终整数最大值;
最终全宽;
最终int颜色1;
最终int color2;
最终整数;
滑杆(
{这是滑球,
这是麦克斯,
这个.min,,
this.fullWidth=false,
这是1,
这是第2条,
这个数字是});
@凌驾
_SliderWidgetState createState();
}
类_SliderWidgetState扩展状态{
双电流值=0;
int get number=>currentValue.round();
@凌驾
小部件构建(构建上下文){
双填充因子=.2;
if(this.widget.fullWidth)paddingFactor=.3;
返回容器(
宽度:this.widget.fullWidth
?双无限
:(此.widget.sliderHeight)*5.5,
高度:(this.widget.sliderHeight),
装饰:新盒子装饰(
borderRadius:新建borderRadius.all(
半径.circular((this.widget.sliderHeight*.3)),
),
渐变:新的线性渐变(
颜色:[
颜色(this.widget.color1),
颜色(this.widget.color2),
],
开始:常量分数偏移(0.0,0.0),
结束:常量分数偏移(1.0,1.00),
停止:[0.0,1.0],
tileMode:tileMode.夹具),
),
孩子:填充(
padding:EdgeInsets.fromLTRB(this.widget.sliderHeight*paddingFactor,
2,this.widget.sliderHeight*填充因子,2),
孩子:排(
儿童:[
正文(
“${this.widget.min}”,
textAlign:textAlign.center,
样式:TextStyle(
fontSize:this.widget.sliderHeight*.3,
fontWeight:fontWeight.w700,
颜色:颜色,白色,
),
),
大小盒子(
宽度:this.widget.sliderHeight*.1,
),
扩大(
儿童:中心(
孩子:幻灯片主题(
数据:SliderTheme.of(context).copyWith(
activeTrackColor:颜色。白色。不透明度(1),
不活动跟踪颜色:颜色。白色。不透明度(.5),
轨道高度:4.0,
拇指形状:CustomSliderThumbCircle(
拇指半径:this.widget.sliderHeight*.4,
darkColor:this.widget.color1,
min:this.widget.min,
马克斯:this.widget.max,
),
覆盖颜色:颜色。白色。不透明度(.4),
),
子:滑块(
值:当前值,
一旦更改:(值){
设置状态(){
当前值=当前值;
});
}),
),
),
),
大小盒子(
宽度:this.widget.sliderHeight*.1,
),
正文(
“${this.widget.max}”,
textAlign:textAlign.center,
样式:TextStyle(
fontSize:this.widget.sliderHeight*.3,
fontWeight:fontWeight.w700,
颜色:颜色,白色,
),
),
],
),
),
);
}
}
编辑:我正在将它打印到这个小部件中的控制台。它位于onPressed参数的底部:

    @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My Mood', style: TextStyle(fontFamily: 'WorkSans')),
        backgroundColor: PrimaryColor,
      ),
      backgroundColor: Color(0xFFF4F1BB),
      body: Column(
        children: <Widget>[
          SizedBox(height: 20),
          //Mood title
          Align(
              alignment: Alignment.center,
              child: Text('How are you feeling?',
                  style: TextStyle(
                      color: Colors.grey[800],
                      fontSize: 20,
                      fontFamily: 'WorkSans'))),
          SizedBox(height: 20),
          //Mood slider
          Align(
              alignment: Alignment.center,
              child:moodSlider),
          SizedBox(height: 20),
          //Sleep title
          Align(
              alignment: Alignment.center,
              child: Text('How long did you sleep?',
                  style: TextStyle(
                      color: Colors.grey[800],
                      fontSize: 20,
                      fontFamily: 'WorkSans'))),
          SizedBox(height: 20),
          //Sleep slider
          Align(
              alignment: Alignment.center,
              child: //SliderWidget(
                  //     sliderHeight: 48,
                  //     min: 0,
                  //     max: 12,
                  //     color1: 0xFF766E87,
                  //     color2: 0xFF766E87)
                  sleepSlider),
          SizedBox(height: 20),
          //Save button
          Align(
              alignment: Alignment.bottomCenter,
              child: RaisedButton(
                child: Text('Save'),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                color: PrimaryColor,
                onPressed: () {
                  print(moodSlider.number);
                }, //_save(moodValue, sleepValue),
              )),
        ],
      ),
    );
  }
@覆盖
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“我的心情”,样式:文本样式(fontFamily:“工作人员”),
背景色:原色,
),
背景颜色:颜色(0xFFF4F1BB),
正文:专栏(
儿童:[
尺寸箱(高度:20),
//语气标题
对齐(
对齐:对齐.center,
孩子:文本(“你感觉怎么样?”,
样式:TextStyle(
颜色:颜色。灰色[800],
尺寸:20,
fontFamily:“工作人员”),
尺寸箱(高度:20),
//情绪滑块
对齐(
对齐:对齐.center,
孩子:摩尔利德),
尺寸箱(高度:20),
//睡眠标题
对齐(
对齐:对齐.center,
孩子:文本(“你睡了多久?”,
样式:TextStyle(
颜色:颜色。灰色[800],
尺寸:20,
fontFamily:“工作人员”),
尺寸箱(高度:20),
//睡眠滑块
对齐(
对齐:对齐.center,
child://SliderWidget(
//幻灯片编号:48,
//分:0,,
//最高:12,
//颜色1:0xFF766E87,
//颜色2:0xFF766E87)
睡眠滑块),
尺寸箱(高度:20),
//保存按钮
对齐(
对齐:对齐.bottomCenter,
孩子:升起按钮(
子项:文本('Save'),
textColor:Colors.white,
int get number => currentValue.round();
class SliderWidget extends StatefulWidget {
  final double sliderHeight;
  final int min;
  final int max;
  final fullWidth;
  final int color1;
  final int color2;
  final int number;         //<-- this one here

  SliderWidget(
      {this.sliderHeight,
      this.max,
      this.min,
      this.fullWidth = false,
      this.color1,
      this.color2,
      this.number});

  @override
  _SliderWidgetState createState() => _SliderWidgetState();
}
class SliderWidget extends StatelessWidget {
  final double sliderHeight;
  final int min;
  final int max;
  final fullWidth;
  final int color1;
  final int color2;
  final double currentValue;
  final ValueChanged<double> onChanged; 
  
  SliderWidget(
      {this.sliderHeight,
      this.max,
      this.min,
      this.fullWidth = false,
      this.color1,
      this.color2,
      this.currentValue, this.onChanged});

  

  @override
  Widget build(BuildContext context) {
    double paddingFactor = .2;

    if ( fullWidth) paddingFactor = .3;

    return Container(
      width:  fullWidth
          ? double.infinity
          : ( sliderHeight) * 5.5,
      height: ( sliderHeight),
      decoration: new BoxDecoration(
        borderRadius: new BorderRadius.all(
          Radius.circular(( sliderHeight * .3)),
        ),
        gradient: new LinearGradient(
            colors: [
              Color( color1),
              Color( color2),
            ],
            begin: const FractionalOffset(0.0, 0.0),
            end: const FractionalOffset(1.0, 1.00),
            stops: [0.0, 1.0],
            tileMode: TileMode.clamp),
      ),
      child: Padding(
        padding: EdgeInsets.fromLTRB( sliderHeight * paddingFactor,
            2,  sliderHeight * paddingFactor, 2),
        child: Row(
          children: <Widget>[
            Text(
              '$min',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize:  sliderHeight * .3,
                fontWeight: FontWeight.w700,
                color: Colors.white,
              ),
            ),
            SizedBox(
              width:  sliderHeight * .1,
            ),
            Expanded(
              child: Center(
                child: SliderTheme(
                  data: SliderTheme.of(context).copyWith(
                    activeTrackColor: Colors.white.withOpacity(1),
                    inactiveTrackColor: Colors.white.withOpacity(.5),
                    trackHeight: 4.0,
                    thumbShape: CustomSliderThumbCircle(
                      thumbRadius:  sliderHeight * .4,
                      darkColor:  color1,
                      min:  min,
                      max:  max,
                    ),
                    overlayColor: Colors.white.withOpacity(.4),
                  ),
                  child: Slider(
                      value: currentValue,
                      onChanged: onChanged),
                ),
              ),
            ),
            SizedBox(
              width:  sliderHeight * .1,
            ),
            Text(
              '$max',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize:  sliderHeight * .3,
                fontWeight: FontWeight.w700,
                color: Colors.white,
              ),
            ),
          ],
        ),
      ),
    );
  }
}