Want to use a builder function in flutter? This is your example with the changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | import 'dart:async'; import 'dart:io'; //import 'package:flutter_appavailability/flutter_appavailability.dart'; typedef Widget MyListTileBuilder(int index); void main() { SystemChrome.setEnabledSystemUIOverlays([]); runApp(Example()); } Future<void> getApp() async { if (Platform.isAndroid) { //installedApps = await AppAvailability.getInstalledApps(); //print(await AppAvailability.checkAvailability("com.android.chrome")); //print(await AppAvailability.isAppEnabled("com.android.chrome")); } else if (Platform.isIOS) { installedApps = iOSApps; //print(await AppAvailability.checkAvailability("calshow://")); } } List<Map<String, String>> installedApp; List<Map<String, String>> installedApps=[ {"app_name":"app1"}, {"app_name":"app2"}, {"app_name":"app3"}, ]; List<Map<String, String>> iOSApps = [ { "app_name": "Calendar", "package_name": "calshow://" }, { "app_name": "Facebook", "package_name": "fb://" }, { "app_name": "Whatsapp", "package_name": "whatsapp://" } ]; class Example extends StatefulWidget { @override ExampleState createState() => ExampleState(); } class ExampleState extends State<Example> { Widget MyListTileBuilderImplementation (int index) { return ListTile ( title: Text(installedApps[index]["app_name"] + " index:" + index.toString()) //this is the text ); } @override Widget build(BuildContext context) { return MaterialApp ( debugShowCheckedModeBanner: false, home: Scaffold ( body: Container ( color: Colors.white, child: AppList (childBuilder: this.MyListTileBuilderImplementation) ) ), ); } } class AppList extends StatefulWidget { @override AppListState createState() => AppListState(); AppList({Key key, this.childBuilder}) : super(key: key); final MyListTileBuilder childBuilder; } class AppListState extends State<AppList> { List<Map<String, String>> _installedApps; @override void initState() { super.initState(); } getApps() { setState(() { installedApps = _installedApps; getApp(); }); } @override Widget build(BuildContext context) { if (installedApps == null) getApps(); return ListView.builder( itemCount: installedApps == null ? 0 : installedApps.length, itemBuilder: (context, index) { return widget.childBuilder(index); }, ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.