【问题标题】:Using syncfusion charts, the chart is not displaying in the screen使用 syncfusion 图表,图表未显示在屏幕上
【发布时间】:2023-02-01 20:44:11
【问题描述】:

创建了一个简单的条形图,但它不会显示在页面上。

这是我的代码

import 'package:flutter/material.dart';
import 'bar_chart_sample1.dart';

class BarChartPage extends StatelessWidget {
  const BarChartPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromARGB(255, 166, 166, 166),
      child: ListView(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(
              left: 28,
              right: 28,
            ),
            child: BarChartWidget(),
          ),
          SizedBox(height: 22),
        ],
      ),
    );
  }
}
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

class BarChartWidget extends StatefulWidget {
  const BarChartWidget({Key? key}) : super(key: key);

  @override
  State<BarChartWidget> createState() => _BarChartWidgetState();
}

class _BarChartWidgetState extends State<BarChartWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
            child: Container(
      child:
          SfCartesianChart(primaryXAxis: CategoryAxis(), series: <ChartSeries>[
        // Initialize line series
        LineSeries<ChartData, String>(
            dataSource: [
              // Bind data source
              ChartData('Jan', 35),
              ChartData('Feb', 28),
              ChartData('Mar', 34),
              ChartData('Apr', 32),
              ChartData('May', 40)
            ],
            xValueMapper: (ChartData data, _) => data.x,
            yValueMapper: (ChartData data, _) => data.y)
      ]),
    )));
  }
}

class ChartData {
  ChartData(this.x, this.y);
  final String x;
  final double? y;
}

它应该看起来像这样

我做错什么了?

我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了? 我做错什么了?

【问题讨论】:

    标签: flutter dart syncfusion


    【解决方案1】:

    您已将脚手架小部件包装在 ListView 中,这就是您遇到此异常的原因。 Scaffold 小部件始终必须是父小部件,或者如果它与任何其他小部件一起包装,我们必须需要指定父小部件的大小。因此,我们建议您避免将 Scaffold 小部件包装在 ListView 中。

    【讨论】:

      【解决方案2】:

      即使我也面临着同样的问题..但我在 Scafold 上没有任何父母.. 该图在 Emulator 中看起来不错,但在 Emulator 中根本没有显示

      @override
        Widget build(BuildContext context) {
          TextTheme _textTheme = Theme.of(context).textTheme;
          // TODO: implement build
          return Scaffold(
            backgroundColor: Colors.white,
            body: Column(
              children: [
                SizedBox(
                  height: 20,
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 10, 20, 10),
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Icon(
                          Icons.arrow_back_ios,
                          color: Colors.blueAccent,
                          size: 20,
                        ),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Image.network(
                        widget.cryptoData.cryptoImage!,
                        height: 20,
                        width: 20,
                        // fit: BoxFit.fill,
                      ),
                      SizedBox(
                        width: 6,
                      ),
                      Text(widget.cryptoData.cryptoName!,
                          style: _textTheme.labelSmall),
                      SizedBox(
                        width: 2,
                      ),
                      Text('(' + widget.cryptoData.cryptoSymbol! + ')',
                          style: _textTheme.titleMedium)
                    ],
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 0, 20, 10),
                  child: Row(
                    children: [
                      Text(
                          "$" +
                              limitDecimalToTwo(
                                  widget.cryptoData.cryptoValue.toString()),
                          style: _textTheme.labelLarge),
                      SizedBox(
                        width: 20,
                      ),
                      Text(
                          limitDecimalToTwo(
                              widget.cryptoData.cryptoValueChange.toString()),
                          style: TextStyle(
                              color:
                                  widget.cryptoData.cryptoValueChange.toString()[0] ==
                                          '-'
                                      ? Colors.red
                                      : Colors.green,
                              fontSize: 14)),
                    ],
                  ),
                ),
                Expanded(
                    flex: 4,
                    child: Container(
                      child: SfCartesianChart(
                        primaryXAxis: CategoryAxis(),
                        primaryYAxis: CategoryAxis(
                            isVisible: false,
                            majorGridLines: const MajorGridLines(width: 0)),
                        series: <ChartSeries<CryptoData, String>>[
                          LineSeries<CryptoData, String>(
                              dataSource: cryptoDataList,
                              xValueMapper: (CryptoData crypto, _) =>
                                  getDateInProperFormat(crypto.date),
                              yValueMapper: (CryptoData crypto, _) => crypto.value),
                        ],
                      ),
                    )),
                SizedBox(
                  height: 20,
                ),
                Padding(
                  padding: const EdgeInsets.all(15),
                  child: Expanded(
                      flex: 2,
                      child: CryptoRowWidget(
                          cryptoName: widget.cryptoData.cryptoName,
                          cryptoCode: widget.cryptoData.cryptoSymbol,
                          cryptoImage: widget.cryptoData.cryptoImage,
                          cryptoValue: widget.cryptoData.cryptoValue.toString(),
                          cryptoValueChange:
                              widget.cryptoData.cryptoValueChange.toString(),
                          cryptoQunatityAvailable: '')),
                ),
                Padding(
                  padding: const EdgeInsets.all(15),
                  child: Expanded(
                    flex: 1,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(shape: StadiumBorder()),
                            onPressed: () {
                              showAlertDialog(context, _textTheme, 'Buy');
                            },
                            child: Padding(
                                padding: const EdgeInsets.fromLTRB(30, 10, 30, 10),
                                child: Text(
                                  'Buy',
                                  style: _textTheme.bodySmall,
                                ))),
                        SizedBox(
                          width: 40,
                        ),
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(shape: StadiumBorder()),
                            onPressed: () {
                              showAlertDialog(context, _textTheme, 'Sell');
                            },
                            child: Padding(
                                padding: const EdgeInsets.fromLTRB(30, 10, 30, 10),
                                child: Text(
                                  'Sell',
                                  style: _textTheme.bodySmall,
                                ))),
                      ],
                    ),
                  ),
                )
              ],
            ),
          );
        }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 2021-09-08
        • 2019-05-25
        • 2019-03-25
        相关资源
        最近更新 更多