{
"id": 1,
"name": "Продукт",
"price": 29.99
}
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
{
"id": 1,
"name": "Олександр",
"email": "[email protected]"
}
class User {
final int id;
final String name;
final String email;
User({required this.id, required this.name, required this.email});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'],
name: json['name'],
email: json['email'],
);
}
}
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
User? user;
bool isLoading = false;
Future<void> fetchUser() async {
setState(() {
isLoading = true;
});
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/users/1'));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
setState(() {
user = User.fromJson(data);
isLoading = false;
});
} else {
setState(() {
isLoading = false;
});
throw Exception('Не вдалося завантажити дані');
}
}
@override
void initState() {
super.initState();
fetchUser();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Flutter JSON та API')),
body: Center(
child: isLoading
? CircularProgressIndicator()
: user != null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('ID: ${user!.id}'),
Text('Ім'я: ${user!.name}'),
Text('Email: ${user!.email}'),
],
)
: Text('Дані відсутні'),
),
);
}
}
class _HomeScreenState extends State<HomeScreen> {
List<User> users = [];
bool isLoading = false;
Future<void> fetchUsers() async {
setState(() {
isLoading = true;
});
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/users'));
if (response.statusCode == 200) {
final List<dynamic> data = jsonDecode(response.body);
setState(() {
users = data.map((json) => User.fromJson(json)).toList();
isLoading = false;
});
} else {
setState(() {
isLoading = false;
});
throw Exception('Не вдалося завантажити список');
}
}
@override
void initState() {
super.initState();
fetchUsers();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Список користувачів')),
body: isLoading
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(users[index].name),
subtitle: Text(users[index].email),
);
},
),
);
}
}
import tkinter as tk
window = tk.Tk()
window.title("Приклад Tkinter")
window.geometry("300x200")
label = tk.Label(window, text="Привіт, це мітка!", font=("Arial", 14))
label.pack(pady=20)
window.mainloop()
import tkinter as tk
def button_click():
label.config(text="Ти натиснув кнопку!")
window = tk.Tk()
window.title("Кнопка Tkinter")
window.geometry("300x200")
label = tk.Label(window, text="Натисни кнопку нижче", font=("Arial", 12))
label.pack(pady=10)
button = tk.Button(window, text="Натисни мене", command=button_click, bg="green", fg="white")
button.pack(pady=10)
window.mainloop()
import tkinter as tk
def show_text():
entered_text = entry.get()
label.config(text=f"Ти ввів: {entered_text}")
window = tk.Tk()
window.title("Текстове поле")
window.geometry("300x200")
label = tk.Label(window, text="Введи щось:", font=("Arial", 12))
label.pack(pady=10)
entry = tk.Entry(window, width=20)
entry.pack(pady=10)
button = tk.Button(window, text="Показати", command=show_text)
button.pack(pady=10)
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.title("Прапорець Tkinter")
window.geometry("300x200")
var1 = tk.BooleanVar()
var2 = tk.BooleanVar()
check1 = tk.Checkbutton(window, text="Варіант 1", variable=var1)
check1.pack(pady=10)
check2 = tk.Checkbutton(window, text="Варіант 2", variable=var2)
check2.pack(pady=10)
def check_status():
label.config(text=f"Варіант 1: {var1.get()}, Варіант 2: {var2.get()}")
button = tk.Button(window, text="Перевірити", command=check_status)
button.pack(pady=10)
label = tk.Label(window, text="")
label.pack(pady=10)
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.title("Список Tkinter")
window.geometry("300x200")
listbox = tk.Listbox(window, height=5)
items = ["Елемент 1", "Елемент 2", "Елемент 3", "Елемент 4"]
for item in items:
listbox.insert(tk.END, item)
listbox.pack(pady=10)
def show_selection():
selected = listbox.get(listbox.curselection())
label.config(text=f"Вибрано: {selected}")
button = tk.Button(window, text="Показати вибір", command=show_selection)
button.pack(pady=10)
label = tk.Label(window, text="")
label.pack(pady=10)
window.mainloop()