Last active
January 29, 2025 10:16
-
-
Save yamnikov-oleg/0573f2d67d426cf9104f38b512cf1e57 to your computer and use it in GitHub Desktop.
Flutter: Рефакторинг
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ЗАДАНИЕ: | |
// Проведите ревью кода приложения. Попробуйте его запустить. | |
// Что можно сделать, чтобы привести его в production-ready вид? | |
import 'package:flutter/material.dart'; | |
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({super.key}); | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
var userAgent = ""; | |
Future<void> loadUserAgent() async { | |
if (userAgent != "") { | |
return; | |
} | |
var url = Uri.https('httpbin.org', 'get'); | |
var response = await http.get(url); | |
print('Response status: ${response.statusCode}'); | |
print('Response body: ${response.body}'); | |
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes)) as Map; | |
var userAgent2 = decodedResponse["headers"]["User-Agent"]; | |
setState(() { | |
userAgent = userAgent2; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
loadUserAgent(); | |
return MaterialApp( | |
title: 'Мой User-Agent', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorSchemeSeed: Colors.blue, | |
), | |
home: Scaffold( | |
appBar: AppBar(title: Text("Мой User-Agent")), | |
body: Column( | |
children: [ | |
Container( | |
padding: EdgeInsets.all(16), | |
child: Container( | |
color: Colors.black12, | |
child: Container( | |
padding: EdgeInsets.all(16), | |
child: Row( | |
children: [ | |
Text(userAgent), | |
SizedBox(width: 16), | |
FilledButton( | |
onPressed: () { | |
setState(() { | |
userAgent = ""; | |
}); | |
}, | |
child: Text("Обновить"), | |
), | |
], | |
), | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment