pubspec.yaml
ํ์ผ์ ํ๋ฌํฐ ํ๋ก์ ํธ์ ๊ด๋ จ๋ ์ค์ ์ ํ๋ ํ์ผ์ด๋ค.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
webview_flutter: ^4.7.0
๐ก flutter pub get์ pubspec.yaml ํ์ผ์ ๋ฑ๋กํ ํ๋ฌ๊ทธ์ธ๋ค์ ๋ด๋ ค๋ฐ๋ ๋ช ๋ น์ด๋ค.
์๋๋ก์ด๋ ์ค์ ํ์ผ์ android/app/src/main/AndroidManifest.xml
์ด๋ค.
AndroidManifest.xml
ํ์ผ์ ์น๋ทฐ๋ฅผ ์คํํ ๋ ์ธํฐ๋ท์ ์ฌ์ฉํด์ผ ํ๋ ์ธํฐ๋ท ๊ถํ์ ์ถ๊ฐํ๋ค.<uses-permission android:name="android.permission.INTERNET" />
android/app/build.gradel
ํ์ผ์ ์ด์ด์ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๋ค.android {
compileSdkVersion 33 โฌ
๏ธ
----- ์๋ต -----
defaultConfig {
applicationId "com.example.blog_web_app"
minSdkVersion 20 โฌ
๏ธ
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
----- ์๋ต -----
}
minSdkVersion
: ์๋๋ก์ด๋ ์ด์์ฒด์ ์ ์ต์ SDK ๋ฒ์ ์ ์ค์ ํ ์ ์๋ ์์นcompileSdkVersion
: ์ฑ์ ๋น๋ํ ๋ ์ฌ์ฉํ SDK ๋ฒ์ ๐ก
android/app/build.gradel
ํ์ผ์ ์์กด์ฑ์ด๋ ๋ฒ์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ค.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="blog_web_app"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"> โฌ
๏ธ
----- ์๋ต -----
</application>
</manifest >
๋ง์ฝ, http ํ๋กํ ์ฝ์ ํ์ฉํ ํ์๊ฐ ์๋ค๋ฉด ํด๋น ์ฝ๋๋ฅผ ์ถ๊ฐํ ํ์๊ฐ ์๋ค.
ios/Runner/Info.plist
ํ์ผ์ http ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ์ค์ ์ ์ถ๊ฐํด์ค๋ค.<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
----- ์๋ต -----
<!-- ์ถ๊ฐํ ์ฝ๋ -->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
</true>
<key>NSAllowsArbitraryLoadsInWebContent</key>
</true>
</dict>
</dict>
</plist>
๐ก
ios/Runner/Info.plist
ํ์ผ์ iOS ์ฑ์ ๋ฐํ์์ ์ค์ ํ๋ ํ์ผ์ด๋ค.
AppBar ์์ ฏ์ ์ผ๋ฐ์ ์ผ๋ก Scaffold ์์ ฏ์ appBar ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ด์ค๋ค.
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
WebViewController controller = WebViewController();
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text('jjinheeWorld'),
centerTitle: true,
),
body: Text('Home Screen'),
);
}
}
WebViewWidget์ ํ๋ฉด์ ์น๋ทฐ๋ฅผ ๋ ๋๋งํด์ฃผ๋ ์ญํ ์ ํ๋ค.
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HomeScreen extends StatelessWidget {
WebViewController controller = WebViewController();
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text('jjinheeWorld'),
centerTitle: true,
),
body: WebViewWidget(
controller: controller,
),
);
}
}
WebViewController๋ ์น๋ทฐ ์์ ฏ์ ์ ์ดํ๋ ์ญํ ์ ํ๋ค.
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HomeScreen extends StatelessWidget {
WebViewController controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse('https://velog.io/@jjinnyit/posts'));
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text('jjinheeWorld'),
centerTitle: true,
),
body: WebViewWidget(
controller: controller,
),
);
}
}
loadRequest()
: Uri ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์
๋ ฅ๋ฐ์ผ๋ฉฐ ์ด ์
๋ ฅ๋ฐ์ ๊ฐ์ ํตํด ์ง์ ํ ์ฌ์ดํธ๋ก ์ด๋StatelessWidget์์ WebViewController๋ฅผ ํ๋กํผํฐ๋ก ์ง์ ์ธ์คํด์คํํ๋ ค๋ฉด WidgetsFlutterBinding.ensureInitialized()
ํจ์๋ฅผ ์ง์ ์คํํด์ฃผ๋ ์์
์ ํด์ผ ํ๋ค.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:blog_web_app/screen/home_screen.dart';
void main() {
// Flutter ํ๋ ์์ํฌ๊ฐ ์ฑ์ ์คํํ ์ค๋น๊ฐ ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
WidgetsFlutterBinding.ensureInitialized();
runApp(
MaterialApp(
home: HomeScreen(),
),
);
}
์ด๋ค ํ์ด์ง์์๋ ์ฐ๋์๋์ ๋ฒจ๋ก๊ทธ ํํ์ด์ง๋ก ๋์์ฌ ์ ์๋ ํ ๋ฒํผ์ ๋ง๋ค์ด๋ณด์.
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HomeScreen extends StatelessWidget {
WebViewController controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse('https://velog.io/@jjinnyit/posts'));
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text('jjinheeWorld'),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
controller.loadRequest(Uri.parse('https://velog.io/@jjinnyit/posts'));
},
icon: Icon(
Icons.home
),
),
],
),
body: WebViewWidget(
controller: controller,
),
);
}
}
์ด ๊ธ์ ๊ณจ๋ ๋๋น ใ์ฝ๋ํฉํ ๋ฆฌ์ ํ๋ฌํฐ ํ๋ก๊ทธ๋๋ฐใ์ ์คํฐ๋ ๋ด์ฉ ์ ๋๋ค.