import 'dart:convert'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:get/get.dart'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'package:piring/bloc/nav/bottom_nav.dart'; import 'package:piring/model/user.dart'; import 'package:shared_preferences/shared_preferences.dart'; class TambahKalori extends StatefulWidget { final String imageUrl; final String judul; const TambahKalori({ super.key, required this.imageUrl, required this.judul, }); @override State createState() => _TambahKaloriState(); } class _TambahKaloriState extends State { List cardValues = []; List> selectedFoods = []; TextEditingController searchController = TextEditingController(); String clientId = "PKL2023"; String clientSecret = "PKLSERU"; String tokenUrl = "https://isipiringku.esolusindo.com/api/Token/token"; String apiUrl = "https://isipiringku.esolusindo.com/api/Konsumsi/Konsumsi"; String accessToken = ""; String Id = ''; Future loadUserData() async { final prefs = await SharedPreferences.getInstance(); final userDataString = prefs.getString('user_data'); if (userDataString != null) { final userData = UserData.fromJson(json.decode(userDataString)); print(userData.nama); setState(() { Id = userData.idUser.toString(); }); } } Future getToken() async { try { // Buat permintaan untuk mendapatkan token menggunakan client_credentials var response = await http.post( Uri.parse(tokenUrl), headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: { 'grant_type': 'client_credentials', 'client_id': clientId, 'client_secret': clientSecret, }, ); if (response.statusCode == 200) { Map tokenData = jsonDecode(response.body); accessToken = tokenData['access_token']; print('Token Akses: $accessToken'); } else { // Handle error, misalnya, menampilkan pesan kesalahan print('Gagal mendapatkan token: ${response.statusCode}'); } } catch (e) { // Handle exception, misalnya, menampilkan pesan kesalahan print('Gagal mendapatkan token: $e'); } } @override void initState() { super.initState(); loadUserData(); getToken(); } @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: BottomNavBar(selected: 1), body: SingleChildScrollView( child: Stack(children: [ Container( height: 130, width: double.infinity, decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/head2.jpg'), fit: BoxFit.cover)), ), SafeArea( child: Padding( padding: EdgeInsets.symmetric(horizontal: 15.0), child: Container( width: MediaQuery.of(context).size.width, child: Stack( children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding(padding: EdgeInsets.only(top: 64)), SizedBox(height: 20), Center( child: Container( width: MediaQuery.of(context).size.height * 0.4, height: 30, decoration: BoxDecoration( gradient: LinearGradient( colors: [ Color.fromARGB(255, 250, 154, 0), Color.fromARGB(255, 246, 80, 20), Color.fromARGB(255, 235, 38, 16), ], ), borderRadius: BorderRadius.circular(30), boxShadow: kElevationToShadow[1], ), padding: const EdgeInsets.symmetric( horizontal: 5, vertical: 0, ), child: Center( child: Text( 'Kalori Harian', style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold, ), ), ), ), ), SizedBox( height: 10, ), ], ), ], ), )), ), ]), ), ); } }