NumberPicker - currentValue不是大胆的

时间:2018-02-10 15:30:23

标签: flutter

我在映射到类的函数中使用NumberPicker包(https://pub.dartlang.org/packages/numberpicker)。它工作正常,但是所选的currentValue不是粗体。我如何分配此整数的值显然存在问题。但我无法弄清楚如何纠正它。

结果图片 - > https://imgur.com/a/mXq6Z

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:numberpicker/numberpicker.dart';

void main() {
  runApp(
    new MaterialApp(
      title: "Test",
      home: new DataPointPage(),
    ),
  );
}

class DatapointsPoint {
  final String type;
  final int point1;
  const DatapointsPoint({this.type, this.point1});
}

class DataPointPage extends StatefulWidget {
  DataPointPage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _DataPointPageState createState() => new _DataPointPageState();
}

class _DataPointPageState extends State<DataPointPage> {
  int total = 100;

  int _currentValue;

  var datapoints = <DatapointsPoint>[
    const DatapointsPoint(type: 'A', point1: 1),
    const DatapointsPoint(type: 'B', point1: 2),
  ];

  Card dataCard(DatapointsPoint data) {
    return new Card(
      child: new Column(
        children: <Widget>[
          new NumberPicker.integer(
            initialValue: _currentValue,
            minValue: 1,
            maxValue: 5,
            onChanged: (newValue) => setState(() {
                  _currentValue = newValue;
                }),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: const Text('myApp'), actions: <Widget>[
        new Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            new Text(
              "Total: $total", // <-- Total the point1 integers
              style: new TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                  fontSize: 20.0),
            ),
          ],
        ),
      ]),
      body: new Column(
          children: datapoints.map((DatapointsPoint data) {
        _currentValue = data.point1; // need to set initial value before passing data??
        return dataCard(data);
      }).toList()),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

我来晚了一点,以防万一其他人参考, 如果initialValue = selectedValue,则只有它会更改颜色。

 NumberPicker levelPicker = new NumberPicker.integer(
    initialValue: _currentLevel,
    minValue: 0,
    maxValue: 100,
    step: 1,
    onChanged: (num) {
      setState(() {
        _currentLevel = num;
        _saveLevel(num);
      });
    });