mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
refactor: split app wrapper from focused example code (multi-file apps)
Convert 17 multi-file apps to the validated pattern: lib/main.dart keeps only the MaterialApp bootstrap; a new lib/example.dart holds the full entry screen and focused example code, importing existing supporting files (screens/, tabs/, services/, models/, widgets/, utils/). Apps: analytics_integration, animation_example, biometrics, bottom_sheet, custom_home_drawer, google_signin, grid_layout, handling_routes, image_editor, scan_qr_code, statless_counter_app, using_bottom_nav_bar, using_custom_fonts, using_listview, using_listwheelscrollview, using_platform_adaptive, view_pdf_file
This commit is contained in:
106
analytics_integration/lib/example.dart
Normal file
106
analytics_integration/lib/example.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
// Example: Analytics tracking with Firebase Analytics showing a list of items
|
||||
import 'package:analytics_integration/single_item_tile.dart';
|
||||
import 'package:firebase_analytics/firebase_analytics.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Example extends StatefulWidget {
|
||||
const Example({super.key});
|
||||
|
||||
/// create instance of FirebaseAnalytics as [analytics]
|
||||
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
|
||||
|
||||
/// create observer for FirebaseAnalytics as [observer]
|
||||
/// this observer sends events to Firebase Analytics when the
|
||||
/// currently active route changes.
|
||||
static FirebaseAnalyticsObserver observer =
|
||||
FirebaseAnalyticsObserver(analytics: analytics);
|
||||
|
||||
@override
|
||||
_ExampleState createState() => _ExampleState();
|
||||
}
|
||||
|
||||
class _ExampleState extends State<Example> {
|
||||
late FirebaseAnalytics _analytics;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
/// initializing data to local variable [_analytics] for Firebase Analytics
|
||||
/// that we made before for local use
|
||||
_analytics = Example.analytics;
|
||||
//// below three events are related to user which we are
|
||||
//// sending to Firebase Analytics
|
||||
_setUserIdInAnalytics();
|
||||
_setUserPropertyInAnalytics();
|
||||
_currentScreen();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Center(
|
||||
child: Text('Flutter Analytics'),
|
||||
),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
children: <Widget>[
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
SingleItemTile(
|
||||
itemName: 'Carrot',
|
||||
analytics: _analytics,
|
||||
quantity: 1,
|
||||
price: '100Rs',
|
||||
),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
SingleItemTile(
|
||||
itemName: 'Baby Carrot',
|
||||
analytics: _analytics,
|
||||
quantity: .5,
|
||||
price: '50Rs',
|
||||
),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//// to create a unique user identifier for Analytics
|
||||
//// send user id(if you app has)
|
||||
Future<void> _setUserIdInAnalytics() async {
|
||||
await _analytics.setUserId(id: 'alksj39hnfn49skvnghqwp40sm');
|
||||
}
|
||||
|
||||
//// sending user related field to Analytics
|
||||
/// below [name] is the name of the user property to set
|
||||
/// [value] is the values of that property
|
||||
Future<void> _setUserPropertyInAnalytics() async {
|
||||
await _analytics.setUserProperty(
|
||||
name: 'email',
|
||||
value: 'johndoe@gmail.com',
|
||||
);
|
||||
}
|
||||
|
||||
/// Setting the current Screen of the app in [screenName]
|
||||
/// and sending back to Analytics
|
||||
Future<void> _currentScreen() async {
|
||||
await _analytics.logEvent(
|
||||
name: 'screen_view',
|
||||
parameters: <String, Object>{
|
||||
'screen_name': 'FlutterAnalyticsHome',
|
||||
'screen_class': 'FlutterAnalyticsHome',
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'package:analytics_integration/single_item_tile.dart';
|
||||
import 'package:firebase_analytics/firebase_analytics.dart';
|
||||
import 'package:analytics_integration/example.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -8,19 +7,10 @@ void main() async {
|
||||
|
||||
/// initialize your firebase project
|
||||
await Firebase.initializeApp();
|
||||
runApp(FlutterAnalyticsApp());
|
||||
runApp(const FlutterAnalyticsApp());
|
||||
}
|
||||
|
||||
class FlutterAnalyticsApp extends StatelessWidget {
|
||||
/// create instance of FirebaseAnalytics as [analytics]
|
||||
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
|
||||
|
||||
/// create observer for FirebaseAnalytics as [observer]
|
||||
/// this observer sends events to Firebase Analytics when the
|
||||
/// currently active route changes.
|
||||
static FirebaseAnalyticsObserver observer =
|
||||
FirebaseAnalyticsObserver(analytics: analytics);
|
||||
|
||||
const FlutterAnalyticsApp({super.key});
|
||||
|
||||
@override
|
||||
@@ -35,114 +25,8 @@ class FlutterAnalyticsApp extends StatelessWidget {
|
||||
|
||||
/// this is used to observe navigation changes in the app
|
||||
/// and sending data back to Firebase Analytics
|
||||
navigatorObservers: <NavigatorObserver>[observer],
|
||||
home: FlutterAnalyticsHome(
|
||||
title: 'Flutter Analytics',
|
||||
analytics: analytics,
|
||||
observer: observer,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class FlutterAnalyticsHome extends StatefulWidget {
|
||||
final String title;
|
||||
final FirebaseAnalytics analytics;
|
||||
final FirebaseAnalyticsObserver observer;
|
||||
|
||||
const FlutterAnalyticsHome({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.analytics,
|
||||
required this.observer,
|
||||
});
|
||||
|
||||
@override
|
||||
_FlutterAnalyticsHomeState createState() => _FlutterAnalyticsHomeState();
|
||||
}
|
||||
|
||||
class _FlutterAnalyticsHomeState extends State<FlutterAnalyticsHome> {
|
||||
late FirebaseAnalytics _analytics;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
/// initializing data to local variable [_analytics] for Firebase Analytics
|
||||
/// that we made before for local use
|
||||
_analytics = widget.analytics;
|
||||
//// below three events are related to user which we are
|
||||
//// sending to Firebase Analytics
|
||||
_setUserIdInAnalytics();
|
||||
_setUserPropertyInAnalytics();
|
||||
_currentScreen();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Center(
|
||||
child: Text(widget.title),
|
||||
),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
children: <Widget>[
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
SingleItemTile(
|
||||
itemName: 'Carrot',
|
||||
analytics: _analytics,
|
||||
quantity: 1,
|
||||
price: '100Rs',
|
||||
),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
SingleItemTile(
|
||||
itemName: 'Baby Carrot',
|
||||
analytics: _analytics,
|
||||
quantity: .5,
|
||||
price: '50Rs',
|
||||
),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//// to create a unique user identifier for Analytics
|
||||
//// send user id(if you app has)
|
||||
Future<void> _setUserIdInAnalytics() async {
|
||||
await _analytics.setUserId(id: 'alksj39hnfn49skvnghqwp40sm');
|
||||
}
|
||||
|
||||
//// sending user related field to Analytics
|
||||
/// below [name] is the name of the user property to set
|
||||
/// [value] is the values of that property
|
||||
Future<void> _setUserPropertyInAnalytics() async {
|
||||
await _analytics.setUserProperty(
|
||||
name: 'email',
|
||||
value: 'johndoe@gmail.com',
|
||||
);
|
||||
}
|
||||
|
||||
/// Setting the current Screen of the app in [screenName]
|
||||
/// and sending back to Analytics
|
||||
Future<void> _currentScreen() async {
|
||||
await _analytics.logEvent(
|
||||
name: 'screen_view',
|
||||
parameters: <String, Object>{
|
||||
'screen_name': 'FlutterAnalyticsHome',
|
||||
'screen_class': 'FlutterAnalyticsHome',
|
||||
},
|
||||
navigatorObservers: <NavigatorObserver>[Example.observer],
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user