Hey can we add SfCartesianChart in Scrollview widget?

I Try to add  SfCartesianChart in Scrollview widget but the chart doesnt exist, no error but the chart still doesnt exist

import 'package:flutter/material.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(
        title: 'Malaria Meter',
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({required this.title});

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  late List<GDPData> _chartData;
  PanelController _panelController = PanelController();

  @override
  void initState() {
    _chartData = getChartData();
    super.initState();
  }

  void togglePanel() => _panelController.isPanelOpen
      ? _panelController.close()
      : _panelController.open();

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red.shade300,
          centerTitle: true,
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {},
          ),
          title: Text(
            widget.title,
          ),
        ),
        body: SlidingUpPanel(
          controller: _panelController,
          maxHeight: size.height * 0.8,
          minHeight: size.height * 0.05,
          body: Center(
            child: Text("Ini adalah body"),
          ),
          panelBuilder: (controller) {
            return SingleChildScrollView(
              controller: controller,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  GestureDetector(
                    onTap: togglePanel,
                    child: Center(
                      child: Container(
                        margin: EdgeInsets.only(top: 15),
                        height: 10,
                        width: 60,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: Colors.grey,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(25),
                    child: Text("Ini adalah item 1"),
                  ),
                  Container(
                    child: SafeArea(
                      child: Scaffold(
                          body: SfCartesianChart(
                        series: <ChartSeries>[
                          BarSeries<GDPData, String>(
                            dataSource: _chartData,
                            xValueMapper: (GDPData gdp, _) => gdp.continent,
                            yValueMapper: (GDPData gdp, _) => gdp.gdp,
                          )
                        ],
                        primaryXAxis: CategoryAxis(),
                      )),
                    ),
                  ),
                ],
              ),
            );
          },
          borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
        ),
      ),
    );
  }

  List<GDPData> getChartData() {
    final List<GDPData> chartData = [
      GDPData('Oceania', 1600),
      GDPData('Africa', 2490),
      GDPData('S America', 2900),
      GDPData('Europe', 23050),
      GDPData('N America', 24880),
      GDPData('Asia', 34390),
    ];
    return chartData;
  }
}

class GDPData {
  GDPData(this.continent, this.gdp);
  final String continent;
  final double gdp;
}



1 Reply 1 reply marked as answer

YG Yuvaraj Gajaraj Syncfusion Team January 23, 2022 03:30 PM UTC

Hi Abram, 
 
Greetings from Syncfusion. We have analyzed your query with the provided code snippet and the size is not obtained properly. So, we suggest removing Scaffold widget, which is parent to chart widget, since you have already a scaffold widget. In other way you set the size to chart widget. Here we have set the size to chart widget using SizedBox widget. Find the code to accomplish this below and this can e changed based on your scenario. 
 
Code snippet: 
SizedBox( 
  heightMediaQuery.of(context).size.width, 
  widthMediaQuery.of(context).size.height, 
  childSafeArea( 
    childScaffold( 
      bodySfCartesianChart( 
       // Other required properties 
     )), 
  ), 
), 
  
Screenshot: 
 
  
Thanks, 
Yuvaraj. 


Marked as answer
Loader.
Up arrow icon