to_csv 1.0.9 to_csv: ^1.0.9 copied to clipboard
This flutter package will help you to create a csv/excel file for your data in flutter framework application.
import 'package:flutter/material.dart';
import 'package:to_csv/to_csv.dart' as exportCSV;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<String> header = []; //Header list variable
List<List<String>> listOfLists = [];
List<String> data1 = [
'1',
'Bilal Saeed',
'1374934',
'912839812'
]; //Inner list which contains Data i.e Row
List<String> data2 = [
'2',
'Ahmar',
'21341234',
'192834821'
]; //Inner list which contains Data i.e Row//Outter List which contains the data List
@override
void initState() {
// TODO: implement initState
super.initState();
header.add('No.');
header.add('User Name');
header.add('Mobile');
header.add('ID Number');
listOfLists.add(header);
listOfLists.add(data1);
listOfLists.add(data2);
}
Widget HeaderRow() {
return Padding(
padding: const EdgeInsets.only(left:17,top:12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(header[0], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(header[1], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(header[2], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(header[3], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Container(
child: Column(
children: [
HeaderRow(),
Divider(),
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: ListView(
children: [
Padding(
padding: const EdgeInsets.only(top:12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(data1[0], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(data1[1], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(data1[2], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(data1[3], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
],
),
),
Padding(
padding: const EdgeInsets.only(top:12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(data2[0], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(data2[1], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(data2[2], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
Text(data2[3], style: const TextStyle(
fontSize: 14, color: Colors.black87, fontWeight: FontWeight.bold,),),
],
),
),
],
),
),
],
),
),
),
floatingActionButton: Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
width: 130,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(12)
),
child: TextButton(
child: const Text('Export CV',style: TextStyle(color: Colors.white,fontSize: 18,fontWeight: FontWeight.bold),),
onPressed: () {
exportCSV.myCSV(header, listOfLists);
},
),)
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}