🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / FLUTTER / home screen widget kullanımı Telefonun ana ekranına widget koyma

1-) FLUTTER - home screen widget kullanımı Telefonun ana ekranına widget koyma

 

hata : https://learnpainless.com/flutter/flutter-workmanager-not-working-in-release-mode-working-in-debug-mode/

 

kaynak : https://pub.dev/packages/home_widget

kaynak : https://medium.com/@ashishgarg1998/how-to-create-home-screen-app-widgets-in-flutter-ce3458f3638e

https://medium.com/@arunshaik432/how-to-create-homescreen-app-widgets-in-flutter-for-both-android-and-ios-travel-schedule-app-cffa601512e6

 

flutter pub add home_widget

 

NOT UYGULAMA : flutter run --profile İLE ÇALIŞMAK ZORUNDA ama --release mode için çözümü buldum. en aşağıdadır.

 

 

 

EKRAN GÖRÜNTÜSÜ

 

 

1- main.dart

 

import 'package:home_widget/home_widget.dart';

 

Future<void> backgroundCallback(Uri? uri) async {
  
if (uri!.host == 'updatecounter') {
  
    
await HomeWidget.getWidgetData<String>('saat', defaultValue: "0")
        .then((value) {
      
DateTime now = new DateTime.now();
      
String hour = now.hour.toString();
      
String minute = now.minute.toString();
      
String second = now.second.toString();
      
String saat = hour + ":" + minute + ":" + second;

      
TumBankaController tum = new TumBankaController(); // apiye baglanma

      tum.getTumBanka().then((List<TumBankaModel> tumbankaModel) async {
        
await HomeWidget.saveWidgetData<String>('saat', saat);
        
await HomeWidget.updateWidget(
            name:
'AppWidgetProvider', iOSName: 'AppWidgetProvider');
      });
    });
  }
}

Future<bool> updateWidget() async {
  
bool? donen = await HomeWidget.updateWidget(
    name:
'AppWidgetProvider',
    iOSName:
'AppWidgetProvider',
  );
  
return Future(() => donen!);
}

Future<void> main() async {
  
WidgetsFlutterBinding.ensureInitialized();

  
try {

   // HomeWidget.setAppGroupId('bankaID12');
    HomeWidget.registerBackgroundCallback(backgroundCallback);
}
catch (ex) {
    print(
"hata " + ex.toString());
  }
}

 

 

 

2- Androidmanifest.xml

 

 

<intent-filter>
    <
action android:name="es.antonborri.home_widget.action.LAUNCH" />
</
intent-filter>

 

</activity>


<!-- Your Background receiver and service goes here -->
<receiver android:name="AppWidgetProvider" android:exported="true">
    <
intent-filter>
        <
action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </
intent-filter>
    <
meta-data android:name="android.appwidget.provider"
        
android:resource="@xml/widget_info" />
</
receiver>

<
receiver android:name="es.antonborri.home_widget.HomeWidgetBackgroundReceiver" android:exported="true">
    <
intent-filter>
        <
action android:name="es.antonborri.home_widget.action.BACKGROUND" />
    </
intent-filter>
</
receiver>

<
service android:name="es.antonborri.home_widget.HomeWidgetBackgroundService"
    
android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>

<
meta-data
    
android:name="flutterEmbedding"
    
android:value="2" />

 

 

 

3- AppWidgetProvider.kt

 

package com.ramzey.bankadovizpro

import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetBackgroundIntent
import es.antonborri.home_widget.HomeWidgetLaunchIntent
import es.antonborri.home_widget.HomeWidgetProvider

class AppWidgetProvider : HomeWidgetProvider() {
    
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, widgetData: SharedPreferences) {
        appWidgetIds.forEach
{ widgetId ->
            
val views = RemoteViews(context.packageName, R.layout.widget_layout).apply {

                
// Open App on Widget Click
                
val pendingIntent = HomeWidgetLaunchIntent.getActivity(context,
                    MainActivity::
class.java)
                setOnClickPendingIntent(
R.id.widget_root, pendingIntent)

                
val saat = widgetData.getString("saat", "15:00:45")
                setTextViewText(
R.id.txtSaat, saat)
               
// Pending intent to update counter on button click
                
val backgroundIntent = HomeWidgetBackgroundIntent.getBroadcast(context,
                    
Uri.parse("myAppWidget://updatecounter"))

                setOnClickPendingIntent(
R.id.bt_update, backgroundIntent)
            
}
            
appWidgetManager.updateAppWidget(widgetId, views)
        
}
    
}
}

 

 

 

4- src -> res -> layout -> widget_layout.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
xmlns:app="http://schemas.android.com/apk/res-auto"
    
xmlns:tools="http://schemas.android.com/tools"
    
android:layout_width="match_parent"
    
android:layout_height="wrap_content">


    <
LinearLayout
        
android:id="@+id/widget_root"
        
android:layout_width="match_parent"
        
android:layout_height="145dp"
        
android:background="@color/purple_500"
        
android:orientation="vertical"
        
android:padding="5dp">

        <
LinearLayout
            
android:layout_width="match_parent"
            
android:layout_height="100dp"
            
android:baselineAligned="false"
            
android:orientation="horizontal"
            
android:weightSum="100">

            <
LinearLayout
                
android:layout_width="0dp"
                
android:layout_height="match_parent"
                
android:layout_marginBottom="5dp"
                
android:layout_weight="30"
                
android:gravity="center"
                
android:orientation="vertical">

                <
TextView
                    
android:id="@+id/txtBankaAd"
                    
android:layout_width="match_parent"
                    
android:layout_height="wrap_content"
                    
android:gravity="center_horizontal"
                    
android:text="HAREM ALTIN"
                    
android:textAlignment="center"
                    
android:textColor="@color/black"
                    
android:textSize="12dp"
                    
android:textStyle="bold" />

                <
TextView
                    
android:id="@+id/txtSaat"
                    
android:layout_width="match_parent"
                    
android:layout_height="wrap_content"
                    
android:gravity="center_horizontal"
                    
android:text="23:02:12"
                    
android:textAlignment="center"
                    
android:textColor="@color/white"
                    
android:textSize="12dp"
                    
android:textStyle="bold" />
            </
LinearLayout>

            <
LinearLayout
                
android:layout_width="0dp"
                
android:layout_height="match_parent"
                
android:layout_weight="70"
                
android:gravity="center"
                
android:orientation="vertical"
                
android:paddingBottom="10dp"
                
android:weightSum="90">

                <
LinearLayout
                    
android:layout_width="match_parent"
                    
android:layout_height="0dp"
                    
android:layout_weight="33"
                    
android:gravity="center"
                    
android:orientation="horizontal"
                    
android:weightSum="90">

                    <
TextView
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="22"
                        
android:text="Altın"
                        
android:textColor="@color/white"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />

                    <
TextView
                        
android:id="@+id/txtAltinAlis"
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="34"
                        
android:text="700"
                        
android:textColor="@color/black"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />

                    <
TextView
                        
android:id="@+id/txtAltinSatis"
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="34"
                        
android:text="800"
                        
android:textColor="@color/black"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />
                </
LinearLayout>

                <
LinearLayout
                    
android:layout_width="match_parent"
                    
android:layout_height="0dp"
                    
android:layout_weight="33"
                    
android:gravity="center"
                    
android:orientation="horizontal"
                    
android:weightSum="90">

                    <
TextView
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="22"
                        
android:text="Dolar"
                        
android:textColor="@color/white"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />

                    <
TextView
                        
android:id="@+id/txtDolarAlis"
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="34"
                        
android:text="700"
                        
android:textColor="@color/black"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />

                    <
TextView
                        
android:id="@+id/txtDolarSatis"
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="34"
                        
android:text="800"
                        
android:textColor="@color/black"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />
                </
LinearLayout>

                <
LinearLayout
                    
android:layout_width="match_parent"
                    
android:layout_height="0dp"
                    
android:layout_weight="33"
                    
android:gravity="center"
                    
android:orientation="horizontal"
                    
android:weightSum="90">

                    <
TextView
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="22"
                        
android:text="Euro"
                        
android:textColor="@color/white"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />

                    <
TextView
                        
android:id="@+id/txtEurAlis"
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="34"
                        
android:text="700"
                        
android:textColor="@color/black"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />

                    <
TextView
                        
android:id="@+id/txtEurSatis"
                        
android:layout_width="0dp"
                        
android:layout_height="wrap_content"
                        
android:layout_weight="34"
                        
android:text="800"
                        
android:textColor="@color/black"
                        
android:textSize="13.5dp"
                        
android:textStyle="bold" />
                </
LinearLayout>

            </
LinearLayout>


        </
LinearLayout>


        <
Button
            
android:id="@+id/bt_update"
            
android:layout_width="match_parent"
            
android:layout_height="wrap_content"
            
android:layout_alignParentBottom="true"
            
android:text="GÜNCELLE"
            
android:textColor="@color/white"
            
android:textStyle="bold"
            
android:backgroundTint="@android:color/holo_green_light"
            
android:textSize="13sp" />
    </
LinearLayout>
</
RelativeLayout>

 

 

5- src -> res -> xml -> widget_info.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    
xmlns:android="http://schemas.android.com/apk/res/android"
    
android:initialLayout="@layout/widget_layout"
    
android:minWidth="180dp"
    
android:minHeight="110dp"
    
android:minResizeWidth="180dp"
    
android:minResizeHeight="110dp"
    
android:widgetCategory="home_screen" />

 

 

 

6- src -> res -> values -> colors.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <
color name="purple_200">#e7e700</color>
    <
color name="purple_500">#cdcd00</color>
    <
color name="purple_700">#b3b300</color>
    <
color name="teal_200">#FF03DAC5</color>
    <
color name="teal_700">#FF018786</color>
    <
color name="black">#FF000000</color>
    <
color name="white">#FFFFFFFF</color>
    <
color name="gri">#fffff0</color>
    <
color name="acik_gri">#e9e9e9</color>
    <
color name="koyu_gri">#6d6d6d</color>
    <
color name="cokkoyu_gri">#959595</color>

    <
color name="acik_sari">#e6e600</color>
</
resources>

 

 

7- release moddaki hata

 

 

Dart Error: Dart_LookupLibrary: library 'package:home_widget/home_widget_callback_dispatcher.dart' not found.

 

 

 

2022-12-04 21:43:49.812 5420-6000/? D/ActivityManager: Received BROADCAST intent 0xaed0829 Key{broadcastIntent pkg=com.ramzey.bankadovizpro intent=act=es.antonborri.home_widget.action.BACKGROUND dat=myAppWidget://updatecounter cmp=com.ramzey.bankadovizpro/es.antonborri.home_widget.HomeWidgetBackgroundReceiver flags=0x4000000 u=0} requestCode=0 sent=0 from uid 10122

2022-12-04 21:43:49.857 5021-5356/? I/HYPER-HAL: [RequestManager.cpp]releaseLocked(): Released ID : 33443946

2022-12-04 21:43:49.904 4944-5361/? E/flutter: [ERROR:flutter/shell/common/shell.cc(93)] Dart Error: Dart_LookupLibrary: library 'package:home_widget/home_widget_callback_dispatcher.dart' not found.

2022-12-04 21:43:49.904 4944-5361/? E/flutter: [ERROR:flutter/runtime/dart_isolate.cc(668)] Could not resolve main entrypoint function.

2022-12-04 21:43:49.904 4944-5361/? E/flutter: [ERROR:flutter/runtime/dart_isolate.cc(167)] Could not run the run main Dart entrypoint.

2022-12-04 21:43:49.908 4944-5361/? E/flutter: [ERROR:flutter/runtime/runtime_controller.cc(385)] Could not create root isolate.

2022-12-04 21:43:49.908 4944-5361/? E/flutter: [ERROR:flutter/shell/common/shell.cc(604)] Could not launch engine with configuration.

 

 

 

AMA RELEASE MODE DA ÇALIŞMASI İÇİN AŞAĞIDAKİLERİ YAPMAK ZORUNDASINIZ

@pragma('vm:entry-point')

 

çözüm : https://stackoverflow.com/questions/73996351/dart-error-dart-lookuplibrary-library-packagebackground-fetch-background-fet


Project -> External Libraies -> Dart Packages -> home_widget-0.1.6 -> home_widget_callback_dispatcher.dart

 

 

 

 2022 Aralık 26 Pazartesi
 1,081