Closed4

Flutterを触る(ネイティブアプリ版Zennの検討?)

dyoshikawadyoshikawa

最初に考えたこと

  • 今のエンジニアリソースではネイティブアプリプロジェクトを別途作り込んでメンテナンスするのは厳しい
  • ガワネイティブが限界そう
    • Flutterでガワネイティブ作ってみる
  • なんでネイティブアプリにしたいの?
    • アプリストアに載せることによるメリット(?)
    • iOS/Androidアプリでコンテンツ回遊したいというユーザーの存在

前提

  • MacBook Apple Chip端末
  • Android StudioではなくVSCodeでコーディングしたい
    • VSCodeインストール済み

Flutter SDK導入

以下を参考にVSCode経由で導入。

https://docs.flutter.dev/get-started/install/macos/mobile-ios

Prompt VS Code to install Flutter

To open the Command Palette, press Command + Shift + P.

In the Command Palette, type flutter.

Select Flutter: New Project.

VS Code prompts you to locate the Flutter SDK on your computer.

If you have the Flutter SDK installed, click Locate SDK.

If you do not have the Flutter SDK installed, click Download SDK.

This option sends you the Flutter install page if you have not installed Git as directed in the development tools prerequisites.

When prompted Which Flutter template?, ignore it. Press Esc. You can create a test project after checking your development setup.

Flutter SDKディレクトリは ~/flutter に配置。

Android Studio導入

以下よりAndroid Studioをダウンロード、インストール。

https://developer.android.com/studio?hl=ja

参考

dyoshikawadyoshikawa

flutter_inappwebviewの導入

https://zenn.dev/taku_zenn/articles/a7ef926c73ec3d

https://github.com/pichillilorenzo/flutter_inappwebview

pubspec.yml
dependencies:
  flutter:
    sdk: flutter
+ flutter_inappwebview: 6.1.5
android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+   <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:label="flutter_application_1"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
+       <meta-data
+          android:name="flutterEmbedding"
+          android:value="2" />
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

dyoshikawadyoshikawa

ZennをWebviewで表示

lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

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

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  InAppWebViewController? webViewController;

  
  void initState() {
    super.initState();
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: null,
      body: InAppWebView(
        initialUrlRequest: URLRequest(url: WebUri("https://zenn.dev")),
        onWebViewCreated: (controller) {
          webViewController = controller;
        },
      ),
    );
  }
}
このスクラップは2024/11/26にクローズされました