๐Ÿ€ Flutter WebView ๋„์ž…๊ธฐ (1) ํ”Œ๋Ÿฌํ„ฐ ์›น๋ทฐ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜

์ด๋„๊ฒฝยท2023๋…„ 5์›” 26์ผ
0

fRONTeND

๋ชฉ๋ก ๋ณด๊ธฐ
10/14
post-thumbnail

Flutter WebView 2ํŽธ

์ง€๋‚œํ•™๊ธฐ ์บก์Šคํ†ค ๋””์ž์ธ์—์„œ WebView๋ฅผ ํ†ตํ•ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐํš์„ ํ•˜์˜€์ง€๋งŒ,, ์ผ์ •์ƒ์˜ ๋ฌธ์ œ๋กœ ํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋ชปํ•œ ์ผ์„ ์ด๋ฒˆ ํ•™๊ธฐ์—๋Š” ํ•ด๋ƒˆ์Šต๋‹ˆ๋‹ค..

์–ด๋””์”€?

์–ด์ฉŒ๋‹ค๋ณด๋‹ˆ ํŒ€ ๊ตฌ์„ฑ์› ์ค‘ ์•ฑ ๊ฐœ๋ฐœ ์Šคํƒ ๋ณด์œ ์ž๋Š” ์—†์—ˆ๊ณ , ์›น ๊ฐœ๋ฐœ์€ ๋งŽ์•˜์Šต๋‹ˆ๋‹ค. (react) ์–ด์ฐŒ์ €์ฐŒํ•˜์—ฌ ํ”Œ๋Ÿฌํ„ฐ๋ฅผ ํ†ตํ•ด ์•ฑ ๋นŒ๋“œ๋ฅผ ์ง€์›ํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

packages

์šฐ์„  ์‚ฌ์šฉ๋œ ํŒจํ‚ค์ง€๋“ค์˜ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.

  cupertino_icons: ^1.0.2
  webview_flutter: ^4.2.0
  path_provider: ^2.0.15
  get: ^4.6.5
  webview_flutter_wkwebview: ^3.4.3
  webview_flutter_android: ^3.7.0
  flutter_native_splash: ^2.3.0
  

Flutter์˜ ๋ฒ„์ „์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  Flutter 3.10.1 โ€ข channel stable โ€ข https://github.com/flutter/flutter.git
  Framework โ€ข revision d3d8effc68 (10 days ago) โ€ข 2023-05-16 17:59:05 -0700
  Engine โ€ข revision b4fb11214d
  Tools โ€ข Dart 3.0.1 โ€ข DevTools 2.23.1

flutter๋„ react์™€ ๊ฐ™์ด ํŒจํ‚ค์ง€ ๋ฒ„์ „๋งˆ๋‹ค ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋ณ€๊ฒฝ๋˜๋Š”๊ฒƒ ๊ฐ™์•„. ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

WebView Controller

์šฐ์„  ์ œ์ผ ์ค‘์š”ํ•œ WebView ์ž…๋‹ˆ๋‹ค.

// webview_controller.dart

//GetX๋ผ๋Š” ์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ปจํŠธ๋กค๋Ÿฌ ํ™œ์šฉ
class WebviewMainController extends GetxController {
//WebviewMainController ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜
  static WebviewMainController get to => Get.find();

  //controller ๊ฐ์ฒด ์„ ์–ธ
  final WebViewController controller = WebViewController()
  
  //์ž๋ฐ”์ŠคํŠธ๋ฆฝํŠธ ์‚ฌ์šฉ ์—ฌ๋ถ€
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    //์›น๋ทฐ ๊ด€๋ จ ์ด๋ฒคํŠธ๋“ค
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          debugPrint('progressing $progress');
        },
        onPageStarted: (String url) {
          debugPrint(url);
        },
        onPageFinished: (String url) {
          debugPrint('Page Finished');
        },
        onWebResourceError: (WebResourceError error) {},
      ),
    )
    //์ ‘์†ํ•œ [URL]์„ ์‚ฝ์ž…
    ..loadRequest(Uri.parse([URL]));

// ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ controller๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•จ
  WebViewController getController() {
    return controller;
  }
}

Main

//main.dart
void main() {
// Flutter ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์œ„์ ฏ ๋ฐ”์ธ๋”ฉ์„ ์ดˆ๊ธฐํ™”
  WidgetsFlutterBinding.ensureInitialized();

  // ๋ฐ˜์‘ํ˜• ์ƒํƒœ ๊ด€๋ฆฌ controller ๋“ฑ๋ก
  Get.put(WebviewMainController());
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
  //WebviewMainController์˜ controller๋ฅผ ํ˜ธ์ถœ
    late final controller = WebviewMainController.to.getController();

    return MaterialApp(
      home: Scaffold(
        appBar: PreferredSize(
        //์•ฑ ๋ฐ”๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์•˜๊ธฐ์— 0์œผ๋กœ
            preferredSize: const Size.fromHeight(0),
            // elevation = ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ทธ๋ฆผ์ž ํšจ๊ณผ 
            child: AppBar(elevation: 0)),
            //WebViewWidget์— controller๋ฅผ parameter๋กœ ๋„˜๊ฒจ์ค€๋‹ค
        body: WebViewWidget(controller: controller),
      ),
    );
  }
}

์ด๋ ‡๊ฒŒํ•˜๋ฉด ๊ธฐ๋ณธ์ ์ธ ์›น๋ทฐ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์™„์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ,, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„ . ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ,
๊ทธ ์ค‘ ์ค‘์š”ํ•œ ๊ฒƒ์€ Flutter - Web ๊ฐ„์˜ ํ†ต์‹ ์ด ์ด๋ค„์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ด๋Š” ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

profile
์•ˆ๋…•ํ•˜์„ธ์šฉ

0๊ฐœ์˜ ๋Œ“๊ธ€