import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:share_plus/share_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';
import 'dart:io';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AiO Search',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const SearchPage(),
);
}
}
class SearchPage extends StatefulWidget {
const SearchPage({Key? key}) : super(key: key);
@override
State<SearchPage> createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
final TextEditingController _searchController = TextEditingController();
late WebViewController _webViewController;
final ScreenshotController _screenshotController = ScreenshotController();
String currentEngine = 'google';
final Map<String, String> searchEngines = {
'naver': 'https://search.naver.com/search.naver?query=',
'daum': 'https://search.daum.net/search?q=',
'google': 'https://www.google.com/search?q=',
'youtube': 'https://www.youtube.com/results?search_query=',
};
@override
void initState() {
super.initState();
_initWebView();
}
void _initWebView() {
_webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse('https://www.google.com'));
}
void _performSearch(String query) {
if (query.isNotEmpty) {
final String searchUrl = '${searchEngines[currentEngine]}${Uri.encodeComponent(query)}';
_webViewController.loadRequest(Uri.parse(searchUrl));
}
// 포커스를 해제합니다.
FocusScope.of(context).unfocus();
}
Future<void> _shareScreenshot() async {
final directory = await getTemporaryDirectory();
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
final String path = '${directory.path}/screenshot_$timestamp.png';
await _screenshotController.captureAndSave(directory.path,
fileName: 'screenshot_$timestamp.png');
await Share.shareFiles([path], text: 'Check out this search result!');
}
Future<void> _shareCurrentUrl() async {
final String? currentUrl = await _webViewController.currentUrl();
if (currentUrl != null) {
await Share.share(currentUrl);
}
}
Future<void> _openInBrowser() async {
final String? currentUrl = await _webViewController.currentUrl();
if (currentUrl != null) {
final Uri url = Uri.parse(currentUrl);
if (await canLaunchUrl(url)) {
await launchUrl(url, mode: LaunchMode.externalApplication);
}
}
}
@override
Widget build(BuildContext context) {
return Screenshot(
controller: _screenshotController,
child: Scaffold(
body: SafeArea(
child: Column(
children: [
// Search bar and engine selection
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border(
bottom: BorderSide(color: Colors.grey.shade300),
),
),
child: Column(
children: [
TextField(
controller: _searchController,
decoration: InputDecoration(
hintText: '검색...',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
contentPadding: const EdgeInsets.symmetric(vertical: 12.0),
),
onSubmitted: _performSearch,
),
const SizedBox(height: 8),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: searchEngines.keys.map((engine) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: TextButton(
onPressed: () {
setState(() => currentEngine = engine);
if (_searchController.text.isNotEmpty) {
_performSearch(_searchController.text);
}
// 버튼을 눌렀을 때 포커스를 해제합니다.
FocusScope.of(context).unfocus();
},
style: TextButton.styleFrom(
backgroundColor: currentEngine == engine
? Colors.blue
: Colors.grey.shade200,
foregroundColor: currentEngine == engine
? Colors.white
: Colors.black,
),
child: Text(engine.toUpperCase()),
),
);
}).toList(),
),
),
],
),
),
// WebView
Expanded(
child: WebViewWidget(controller: _webViewController),
),
// Navigation bar
Container(
padding: const EdgeInsets.symmetric(vertical: 8.0),
decoration: BoxDecoration(
border: Border(top: BorderSide(color: Colors.grey.shade300)),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
_webViewController.goBack();
FocusScope.of(context).unfocus();
},
),
IconButton(
icon: const Icon(Icons.camera_alt),
onPressed: () {
_shareScreenshot();
FocusScope.of(context).unfocus();
},
),
IconButton(
icon: const Icon(Icons.open_in_browser),
onPressed: () {
_openInBrowser();
FocusScope.of(context).unfocus();
},
),
IconButton(
icon: const Icon(Icons.share),
onPressed: () {
_shareCurrentUrl();
FocusScope.of(context).unfocus();
},
),
IconButton(
icon: const Icon(Icons.arrow_forward),
onPressed: () {
_webViewController.goForward();
FocusScope.of(context).unfocus();
},
),
],
),
),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:share_plus/share_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';
import 'dart:io';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AiO Search',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const SearchPage(),
);
}
}
class SearchPage extends StatefulWidget {
const SearchPage({Key? key}) : super(key: key);
@override
State<SearchPage> createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
final TextEditingController _searchController = TextEditingController();
late WebViewController _webViewController;
final ScreenshotController _screenshotController = ScreenshotController();
String currentEngine = 'google';
final Map<String, String> searchEngines = {
'naver': 'https://search.naver.com/search.naver?query=',
'daum': 'https://search.daum.net/search?q=',
'google': 'https://www.google.com/search?q=',
'youtube': 'https://www.youtube.com/results?search_query=',
};
@override
void initState() {
super.initState();
_initWebView();
}
void _initWebView() {
_webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse('https://www.google.com'));
}
void _performSearch(String query) {
if (query.isNotEmpty) {
final String searchUrl = '${searchEngines[currentEngine]}${Uri.encodeComponent(query)}';
_webViewController.loadRequest(Uri.parse(searchUrl));
FocusScope.of(context).unfocus();
}
}
Future<void> _shareScreenshot() async {
final directory = await getTemporaryDirectory();
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
final String path = '${directory.path}/screenshot_$timestamp.png';
await _screenshotController.captureAndSave(directory.path,
fileName: 'screenshot_$timestamp.png');
await Share.shareFiles([path], text: 'Check out this search result!');
}
Future<void> _shareCurrentUrl() async {
final String? currentUrl = await _webViewController.currentUrl();
if (currentUrl != null) {
await Share.share(currentUrl);
}
}
Future<void> _openInBrowser() async {
final String? currentUrl = await _webViewController.currentUrl();
if (currentUrl != null) {
final Uri url = Uri.parse(currentUrl);
if (await canLaunchUrl(url)) {
await launchUrl(url, mode: LaunchMode.externalApplication);
}
}
}
Future<bool> _onWillPop() async {
if (await _webViewController.canGoBack()) {
_webViewController.goBack();
return false; // 뒤로가기를 막음
}
return true; // 앱 종료 허용
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Screenshot(
controller: _screenshotController,
child: Scaffold(
body: SafeArea(
child: Column(
children: [
// Search bar and engine selection
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border(
bottom: BorderSide(color: Colors.grey.shade300),
),
),
child: Column(
children: [
TextField(
controller: _searchController,
decoration: InputDecoration(
hintText: '검색...',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
contentPadding: const EdgeInsets.symmetric(vertical: 12.0),
),
onSubmitted: _performSearch,
),
const SizedBox(height: 8),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: searchEngines.keys.map((engine) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: TextButton(
onPressed: () {
setState(() => currentEngine = engine);
if (_searchController.text.isNotEmpty) {
_performSearch(_searchController.text);
}
FocusScope.of(context).unfocus(); // 포커스 해제
},
style: TextButton.styleFrom(
backgroundColor: Colors.grey.shade200, // 같은 배경색
foregroundColor: Colors.black, // 같은 글자색
),
child: Text(engine.toUpperCase()),
),
);
}).toList(),
),
),
],
),
),
// WebView
Expanded(
child: WebViewWidget(controller: _webViewController),
),
// Navigation bar
Container(
padding: const EdgeInsets.symmetric(vertical: 8.0),
decoration: BoxDecoration(
border: Border(top: BorderSide(color: Colors.grey.shade300)),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => _webViewController.goBack(),
),
IconButton(
icon: const Icon(Icons.camera_alt),
onPressed: _shareScreenshot,
),
IconButton(
icon: const Icon(Icons.open_in_browser),
onPressed: _openInBrowser,
),
IconButton(
icon: const Icon(Icons.share),
onPressed: _shareCurrentUrl,
),
IconButton(
icon: const Icon(Icons.arrow_forward),
onPressed: () => _webViewController.goForward(),
),
],
),
),
],
),
),
),
),
);
}
}