首页 > 解决方案 > How to change font size on flutter using a slider indicator?

问题描述

The Code below only changes the slider from 1-10, how do you change the text while changing the slider as well in Flutter?
The Text is fetched from an API.

 double _value = 0.0;
[enter image description here][1] bool _slider = false;

child: _slider == true

                      ? new Container(
                          margin: EdgeInsets.only(right: 10),
                          decoration: new BoxDecoration(
                              color: NNDColors.main,
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(5.0)),
                              boxShadow: [
                                new BoxShadow(
                                    color: Colors.black38,
                                    offset: new Offset(0.0, 2.0),
                                    blurRadius: 10)
                              ]),
                          child: new Slider(
                            value: _value,
                            activeColor: Colors.white,
                            inactiveColor: Colors.white,
                            onChanged: (double s) => _changed(s),
                            divisions: 10,
                            min: 0.0,
                            max: 10.0,
                          ),

  void _changed(s) {
    setState((){
      _value = s;
    });
  }

标签: flutterdartflutter-layout

解决方案


输出:

在此处输入图像描述

这是你如何做到的。

double _value = 5;

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text("Testing")),
    body: Center(
      child: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(right: 10),
            decoration: new BoxDecoration(
                color: Colors.blue,
                borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
                boxShadow: [new BoxShadow(color: Colors.black38, offset: new Offset(0.0, 2.0), blurRadius: 10)]),
            child: new Slider(
              value: _value,
              activeColor: Colors.white,
              inactiveColor: Colors.white,
              onChanged: (double s) {
                setState(() {
                  _value = s;
                });
              },
              divisions: 10,
              min: 0.0,
              max: 10.0,
            ),
          ),
          Text("Hello World", style: TextStyle(fontSize: 10 * _value)),
        ],
      ),
    ),
  );
}

推荐阅读