WebView
Last updated
Last updated
외부 패키지 활용법
package, open source, library, plugin
webview
WebView란 어떤 웹 사이트를 앱에다 패키징한 것
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: WebView(
initialUrl: 'https://judy-soobin.gitbook.io/soobin/',
javascriptMode: JavascriptMode.unrestricted,
),
)
);
}
}
...
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
# web view library
webview_flutter: ^3.0.0
...
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "judy.flutter.web_view"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 20 // here !!!
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
...
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('Judy Blog'),
centerTitle: true,
),
body: Center(
child: WebView(
initialUrl: 'https://judy-soobin.gitbook.io/soobin/',
javascriptMode: JavascriptMode.unrestricted,
),
)
);
}
}
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HomeScreen extends StatelessWidget {
WebViewController? controller;
final homeUrl = 'https://judy-soobin.gitbook.io/soobin/';
HomeScreen({Key? key}) : super(key : key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('Judy Blog'),
centerTitle: true,
actions: [
IconButton(onPressed: (){
if(controller == null) return;
controller!.loadUrl(homeUrl);
}, icon: Icon(
Icons.home,
))
],
),
body: Center(
child: WebView(
onWebViewCreated: (WebViewController controller){
this.controller = controller;
},
initialUrl: homeUrl,
javascriptMode: JavascriptMode.unrestricted,
),
)
);
}
}
http 허용 세팅
ios
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
<true />
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true />
</dict>
android
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="web_view"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true">
...
import 'package:flutter/material.dart';
import 'package:web_view/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';
final homeUrl = Uri.parse('https://judy-soobin.gitbook.io/soobin/');
class HomeScreen extends StatelessWidget {
WebViewController controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(homeUrl);
HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('Judy Blog'),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
controller.loadRequest(homeUrl);
},
icon: Icon(
Icons.home,
))
],
),
body: WebViewWidget(
controller: controller,
),
);
}
}