import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:http/http.dart' as http;
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(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
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> items = [];
final _googleSignIn = GoogleSignIn(
scopes: [
'https://www.googleapis.com/auth/calendar',
],
);
GoogleSignInAccount? _currentUser;
late Map<String, dynamic> cals;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
setState(() {
_currentUser = account;
if (_currentUser != null) {
_getCalendarList();
}
});
});
_googleSignIn.signIn().then((GoogleSignInAccount? account) {
if (account != null) {
setState(() {
_currentUser = account;
_getCalendarList();
});
}
});
}
void _getCalendarList() async {
http.Client client = http.Client();
var headers = await _currentUser?.authHeaders;
var resp = await client.get(Uri.parse("https://www.googleapis.com/calendar/v3/users/me/calendarList"), headers: headers);
cals = jsonDecode(resp.body);
for(var i=0;i<cals['items'].length;i++) {
print(cals['items'][i]['summary']);
}
}
void _incrementCounter() {
setState(() {
for(var i=0;i<cals['items'].length;i++) {
print(cals['items'][i]['summary']);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}