build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  Future.delayed(const Duration(seconds: 5), () {
    List<KLineEntity> data = [];

    /// KLineEntity list data
    var decode = json.decode(jsonString);
    for (var item in decode) {
      data.add(KLineEntity()
        ..time = item[0]
        ..open = double.parse('${item[1]}')
        ..high = double.parse('${item[2]}')
        ..low = double.parse('${item[3]}')
        ..close = double.parse('${item[4]}')
        ..vol = double.parse('${item[5]}'));
    }

    adapter.resetData(data);
    // Timer.periodic(const Duration(seconds: 3), (timer) {
    //   KLineEntity item = data[Random().nextInt(data.length - 1)];
    //   data.add(KLineEntity()
    //     ..time = item.time
    //     ..open = item.open
    //     ..high = item.high
    //     ..low = item.low
    //     ..close = item.close
    //     ..vol = item.vol);
    //   adapter.resetData(data, resetTranslate: true);
    // });
  });

  return MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
      // This is the theme of your application.
      //
      // Try running your application with "flutter run". You'll see the
      // application has a blue toolbar. Then, without quitting the app, try
      // changing the primarySwatch below to Colors.green and then invoke
      // "hot reload" (press "r" in the console where you ran "flutter run",
      // or simply save your changes to "hot reload" in a Flutter IDE).
      // Notice that the counter didn't reset back to zero; the application
      // is not restarted.
      primarySwatch: Colors.blue,
    ),
    home: Scaffold(
      backgroundColor: Colors.grey,
      body: Column(
        children: [
          Container(
            margin: const EdgeInsets.only(top: 100),
            width: double.maxFinite,
            height: 400,
            child: KChartWidget(
              adapter,
              config: KRunConfig(
                selectedPriceBuilder: (double? value) {
                  return TextSpan(
                    text: value?.toStringAsFixed(2) ?? '--',
                    style: const TextStyle(color: Colors.white),
                  );
                },
                dateFormatter: (int? value) {
                  return formatDate(
                      DateTime.fromMillisecondsSinceEpoch(value ?? 0));
                },
                mainValueFormatter: (number) {
                  return number?.toStringAsFixed(3) ?? '--';
                },
                volValueFormatter: (number) {
                  return number?.toStringAsFixed(3) ?? '--';
                },
                infoBuilder: (klineEntry) {
                  return <TextSpan, TextSpan>{
                    const TextSpan(text: 'Date'): TextSpan(
                        text: formatDate2(
                      DateTime.fromMillisecondsSinceEpoch(
                          klineEntry.time ?? 0),
                    )),
                    const TextSpan(text: 'open'):
                        TextSpan(text: klineEntry.open.toStringAsFixed(3)),
                    const TextSpan(text: 'high'):
                        TextSpan(text: klineEntry.high.toStringAsFixed(3)),
                    const TextSpan(text: 'low'):
                        TextSpan(text: klineEntry.low.toStringAsFixed(3)),
                    const TextSpan(text: 'close'):
                        TextSpan(text: klineEntry.close.toStringAsFixed(3)),
                    const TextSpan(text: 'vol'):
                        TextSpan(text: klineEntry.vol.toStringAsFixed(3)),
                  };
                },
              ),
            ),
          ),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () => adapter.changeType(MainDisplayType.boll),
                  child: const Text("BOLL")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(MainDisplayType.ma),
                  child: const Text("MA")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(MainDisplayType.none),
                  child: const Text("不显示")),
            ],
          ),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartSenType.macd),
                  child: const Text("MACD")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartSenType.kdj),
                  child: const Text("KDJ")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartSenType.rsi),
                  child: const Text("RSI")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartSenType.cci),
                  child: const Text("CCI")),
            ],
          ),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () =>
                      adapter.changeType(ChartGroupType.withNone),
                  child: const Text("K线")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartGroupType.withSen),
                  child: const Text("附图")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartGroupType.withVol),
                  child: const Text("成交")),
              ElevatedButton(
                  onPressed: () =>
                      adapter.changeType(ChartGroupType.withVolSen),
                  child: const Text("附图和成交量")),
            ],
          ),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () => adapter.changeType(ChartDisplayType.kline),
                  child: const Text("显示K线")),
              ElevatedButton(
                  onPressed: () =>
                      adapter.changeType(ChartDisplayType.timeLine),
                  child: const Text("显示分时线")),
            ],
          ),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () => adapter.changeType(CrossType.followClose),
                  child: const Text("十字线收盘价")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(CrossType.followFinger),
                  child: const Text("十字线手指")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(CrossType.followAll),
                  child: const Text("手指和收盘价")),
            ],
          ),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () => adapter.changeType(XAxisType.flow),
                  child: const Text("X坐标滑动")),
              ElevatedButton(
                  onPressed: () => adapter.changeType(XAxisType.pin),
                  child: const Text("X坐标固定")),
            ],
          )
        ],
      ),
    ),
  );
}