Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adding App Widget with Flutter - Part 1 (Android)

Adding App Widget with Flutter - Part 1 (Android)

This is a slide on potatotips 76.
「Adding App Widget with Flutter - Part 1 (Android)」
#potatotips #flutterjp #flutter #androidjp

Kenichi Kambara

November 17, 2021
Tweet

More Decks by Kenichi Kambara

Other Decks in Technology

Transcript

  1. About me •Mobile App Development •Speeches (e.g. 9 International confs.)

    •Writings (e.g. 5 Dev Books) •[Of fi cial] Evangelist at NTT TechnoCross •[Private] iplatform.org Kenichi Kambara (@korodroid)
  2. Approach  •Native [Android] 1. Create App Widget Layout 2.

    Create App Widget Con fi guration 3. Add App Provider Widget 4. Add some de fi nition on AndroidManifest •Flutter 5. Implement Dart code
  3. 1. Create App Widget Layout <LinearLayout xmlns:android="http://schemas.android.com/apk/ res/android" android:layout_width="match_parent" android:layout_height="match_parent"

    android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_margin="4dp" android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/chara1" /> <…>  <android_src>/res/layout/: sample_widget_layout.xml
  4. 2. Create App Widget Con fi guration <?xml version="1.0" encoding="utf-8"?>

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout=“@layout/sample_widget_layout" android:minWidth="320dp" android:minHeight="40dp" android:minResizeWidth="320dp" android:minResizeHeight="40dp" android:widgetCategory="home_screen" />  <android_src>/res/xml/: sample_widget_con fi g.xml
  5. [Appendix] App Widget Size  the table below to roughly

    estimate ref: https://stuff.mit.edu/afs/sipb/project/android/docs/guide/practices/ui_guidelines/widget_design.html $FMM 4J[F  EQ  EQ ʜ ʜ O  O
  6. 3. Add App Widget Provider class SampleAppWidgetProvider : HomeWidgetProvider() {

    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, widgetData: SharedPreferences) { appWidgetIds.forEach { widgetId -> val views = RemoteViews(context.packageName, R.layout.sample_widget_layout).apply { var srcText = widgetData.getString("_srcText", ""); setTextViewText(R.id.srcText, srcText) } appWidgetManager.updateAppWidget(widgetId, views) } } }  <android_src>/: SampleAppWidgetProvider.kt
  7. 4. Add some de fi nition on AndroidManifest <!-- for

    User-Defined Class--> <receiver android:name=".SampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/sample_widget_config" /> </receiver>  <android_src>/: AndroidManifest.xml
  8. 5. Flutter Coding Future<void> updateAppWidget() async { HomeWidget.saveWidgetData<String>('_srcText', "<Data from

    Dart>>"); HomeWidget.updateWidget( name: 'SampleAppWidgetProvider', 
 iOSName: 'SampleAppWidgetExtention'); }  < fl utter_src>/: hoge.dart
  9. Please let me know if you have any requests 


    such as technical speeches, technical writings and so on. Facebook:http://fb.com/kanbara.kenichi Twitter:@korodroid LinkedIn:http://www.linkedin.com/in/korodroid Thank you so much