From 6e3ff94bfe94cd0fa865ec6f1cfeb187d0a4ca79 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Tue, 18 Aug 2026 00:48:50 +0200 Subject: [PATCH] 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 --- analytics_integration/lib/example.dart | 106 ++++++++++++++ analytics_integration/lib/main.dart | 124 +---------------- .../lib/{home_page.dart => example.dart} | 17 ++- animation_example/lib/main.dart | 5 +- biometrics/lib/example.dart | 88 ++++++++++++ biometrics/lib/main.dart | 91 +----------- bottom_sheet/lib/example.dart | 12 ++ bottom_sheet/lib/main.dart | 7 +- custom_home_drawer/lib/example.dart | 12 ++ custom_home_drawer/lib/main.dart | 4 +- google_signin/lib/example.dart | 76 +++++++++++ google_signin/lib/main.dart | 73 +--------- grid_layout/lib/example.dart | 22 +++ grid_layout/lib/main.dart | 16 +-- .../lib/{screens/home.dart => example.dart} | 9 +- handling_routes/lib/main.dart | 5 +- image_editor/lib/example.dart | 129 ++++++++++++++++++ image_editor/lib/main.dart | 10 +- scan_qr_code/lib/example.dart | 80 +++++++++++ scan_qr_code/lib/main.dart | 82 +---------- statless_counter_app/lib/example.dart | 42 ++++++ statless_counter_app/lib/main.dart | 44 +----- using_bottom_nav_bar/lib/example.dart | 75 ++++++++++ using_bottom_nav_bar/lib/main.dart | 80 ++--------- using_custom_fonts/lib/example.dart | 31 +++++ using_custom_fonts/lib/main.dart | 29 +--- using_listview/lib/example.dart | 17 +++ using_listview/lib/main.dart | 10 +- using_listwheelscrollview/lib/example.dart | 12 ++ using_listwheelscrollview/lib/main.dart | 42 +++--- using_platform_adaptive/lib/example.dart | 123 +++++++++++++++++ using_platform_adaptive/lib/main.dart | 124 +---------------- view_pdf_file/lib/example.dart | 84 ++++++++++++ view_pdf_file/lib/main.dart | 87 +----------- 34 files changed, 1002 insertions(+), 766 deletions(-) create mode 100644 analytics_integration/lib/example.dart rename animation_example/lib/{home_page.dart => example.dart} (90%) create mode 100644 biometrics/lib/example.dart create mode 100644 bottom_sheet/lib/example.dart create mode 100644 custom_home_drawer/lib/example.dart create mode 100644 google_signin/lib/example.dart create mode 100644 grid_layout/lib/example.dart rename handling_routes/lib/{screens/home.dart => example.dart} (85%) create mode 100644 image_editor/lib/example.dart create mode 100644 scan_qr_code/lib/example.dart create mode 100644 statless_counter_app/lib/example.dart create mode 100644 using_bottom_nav_bar/lib/example.dart create mode 100644 using_custom_fonts/lib/example.dart create mode 100644 using_listview/lib/example.dart create mode 100644 using_listwheelscrollview/lib/example.dart create mode 100644 using_platform_adaptive/lib/example.dart create mode 100644 view_pdf_file/lib/example.dart diff --git a/analytics_integration/lib/example.dart b/analytics_integration/lib/example.dart new file mode 100644 index 0000000..e2601f6 --- /dev/null +++ b/analytics_integration/lib/example.dart @@ -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 { + 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: [ + 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 _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 _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 _currentScreen() async { + await _analytics.logEvent( + name: 'screen_view', + parameters: { + 'screen_name': 'FlutterAnalyticsHome', + 'screen_class': 'FlutterAnalyticsHome', + }, + ); + } +} diff --git a/analytics_integration/lib/main.dart b/analytics_integration/lib/main.dart index 389d381..6b31253 100644 --- a/analytics_integration/lib/main.dart +++ b/analytics_integration/lib/main.dart @@ -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: [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 { - 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: [ - 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 _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 _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 _currentScreen() async { - await _analytics.logEvent( - name: 'screen_view', - parameters: { - 'screen_name': 'FlutterAnalyticsHome', - 'screen_class': 'FlutterAnalyticsHome', - }, + navigatorObservers: [Example.observer], + home: const Example(), ); } } diff --git a/animation_example/lib/home_page.dart b/animation_example/lib/example.dart similarity index 90% rename from animation_example/lib/home_page.dart rename to animation_example/lib/example.dart index 2a693a8..52bd713 100644 --- a/animation_example/lib/home_page.dart +++ b/animation_example/lib/example.dart @@ -1,14 +1,15 @@ +// Example: A compass-like animation demo where tapping rotates a pen image +// and points it toward a direction (north/west/south/east). import 'package:flutter/material.dart'; -class HomePage extends StatefulWidget { - const HomePage({super.key}); +class Example extends StatefulWidget { + const Example({super.key}); @override - _HomePageState createState() => _HomePageState(); + _ExampleState createState() => _ExampleState(); } -class _HomePageState extends State - with SingleTickerProviderStateMixin { +class _ExampleState extends State with SingleTickerProviderStateMixin { late AnimationController controller; var target = 0.0; final Map data = { @@ -54,8 +55,7 @@ class _HomePageState extends State child: TextButton( onPressed: () {}, style: ButtonStyle( - backgroundColor: - WidgetStateProperty.all(Colors.green)), + backgroundColor: WidgetStateProperty.all(Colors.green)), child: const Text( "North", style: TextStyle( @@ -104,8 +104,7 @@ class _HomePageState extends State child: TextButton( onPressed: () {}, style: ButtonStyle( - backgroundColor: - WidgetStateProperty.all(Colors.green)), + backgroundColor: WidgetStateProperty.all(Colors.green)), child: const Text( "North", style: TextStyle( diff --git a/animation_example/lib/main.dart b/animation_example/lib/main.dart index b7bf8f3..6d63bb6 100644 --- a/animation_example/lib/main.dart +++ b/animation_example/lib/main.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; -import 'home_page.dart'; +import 'example.dart'; void main() { runApp(const MyApp()); @@ -15,10 +15,9 @@ class MyApp extends StatelessWidget { return MaterialApp( title: 'Pen Assignement', theme: ThemeData( - primarySwatch: Colors.blue, ), - home:HomePage(), + home: const Example(), ); } } diff --git a/biometrics/lib/example.dart b/biometrics/lib/example.dart new file mode 100644 index 0000000..d38233f --- /dev/null +++ b/biometrics/lib/example.dart @@ -0,0 +1,88 @@ +// Example: Biometric verification screen +import 'package:biometrics/biometrics_verifier.dart'; +import 'package:flutter/material.dart'; + +class Example extends StatefulWidget { + const Example({super.key}); + + @override + State createState() => _ExampleState(); +} + +class _ExampleState extends State { + late bool isVerified; + + late BiometricsVerifier verifier; + + @override + void initState() { + super.initState(); + isVerified = false; + verifier = BiometricsVerifier(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(), + body: SizedBox( + width: double.infinity, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + isVerified ? 'Verification Complete' : 'Unverified', + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(height: 10), + isVerified + ? TextButton( + onPressed: () { + setState(() { + isVerified = false; + }); + }, + child: const Text('Unverify'), + ) + : TextButton( + onPressed: () async { + try { + await verifier + .verifyBiometrics('Please enter your fingerprint'); + // ---- Add your logic after finger print verification here + // --- + // --- + setState(() { + isVerified = true; + }); + } catch (e) { + // ---- Verification Failed + if (!context.mounted) return; + showDialog( + context: context, + builder: (c) => AlertDialog( + title: const Text('Error !'), + content: Text(e.toString()), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('Ok'), + ) + ], + ), + ); + } + }, + child: const Text('Verify with Fingerprint'), + ), + ], + ), + ), + ); + } +} diff --git a/biometrics/lib/main.dart b/biometrics/lib/main.dart index 06c7e21..2604104 100644 --- a/biometrics/lib/main.dart +++ b/biometrics/lib/main.dart @@ -1,7 +1,7 @@ -import 'package:biometrics/biometrics_verifier.dart'; +import 'package:biometrics/example.dart'; import 'package:flutter/material.dart'; -void main() => runApp(Biometrics()); +void main() => runApp(const Biometrics()); class Biometrics extends StatelessWidget { const Biometrics({super.key}); @@ -13,92 +13,7 @@ class Biometrics extends StatelessWidget { theme: ThemeData( appBarTheme: const AppBarTheme(elevation: 0), ), - home: Home(), - ); - } -} - -class Home extends StatefulWidget { - const Home({super.key}); - - @override - State createState() => _HomeState(); -} - -class _HomeState extends State { - late bool isVerified; - - late BiometricsVerifier verifier; - - @override - void initState() { - super.initState(); - isVerified = false; - verifier = BiometricsVerifier(); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(), - body: SizedBox( - width: double.infinity, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - isVerified ? 'Verification Complete' : 'Unverified', - style: const TextStyle( - fontSize: 16, - fontWeight: FontWeight.bold, - ), - ), - const SizedBox(height: 10), - isVerified - ? TextButton( - onPressed: () { - setState(() { - isVerified = false; - }); - }, - child: const Text('Unverify'), - ) - : TextButton( - onPressed: () async { - try { - await verifier - .verifyBiometrics('Please enter your fingerprint'); - // ---- Add your logic after finger print verification here - // --- - // --- - setState(() { - isVerified = true; - }); - } catch (e) { - // ---- Verification Failed - if (!context.mounted) return; - showDialog( - context: context, - builder: (c) => AlertDialog( - title: const Text('Error !'), - content: Text(e.toString()), - actions: [ - TextButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: const Text('Ok'), - ) - ], - ), - ); - } - }, - child: const Text('Verify with Fingerprint'), - ), - ], - ), - ), + home: const Example(), ); } } diff --git a/bottom_sheet/lib/example.dart b/bottom_sheet/lib/example.dart new file mode 100644 index 0000000..222c4aa --- /dev/null +++ b/bottom_sheet/lib/example.dart @@ -0,0 +1,12 @@ +// Example: Bottom sheet +import 'package:bottom_sheet/home.dart'; +import 'package:flutter/material.dart'; + +class Example extends StatelessWidget { + const Example({super.key}); + + @override + Widget build(BuildContext context) { + return const MyHomePage(title: 'Bottom Sheet'); + } +} diff --git a/bottom_sheet/lib/main.dart b/bottom_sheet/lib/main.dart index 3f21778..412ef19 100644 --- a/bottom_sheet/lib/main.dart +++ b/bottom_sheet/lib/main.dart @@ -1,8 +1,8 @@ +import 'package:bottom_sheet/example.dart'; import 'package:flutter/material.dart'; -import 'package:bottom_sheet/home.dart'; void main() { - runApp(MyApp()); + runApp(const MyApp()); } class MyApp extends StatelessWidget { @@ -17,8 +17,7 @@ class MyApp extends StatelessWidget { primarySwatch: Colors.red, visualDensity: VisualDensity.adaptivePlatformDensity, ), - home: MyHomePage(title: 'Bottom Sheet'), + home: const Example(), ); } } - diff --git a/custom_home_drawer/lib/example.dart b/custom_home_drawer/lib/example.dart new file mode 100644 index 0000000..ea285af --- /dev/null +++ b/custom_home_drawer/lib/example.dart @@ -0,0 +1,12 @@ +// Example: Custom home drawer +import 'package:custom_home_drawer/screen/home_screen.dart'; +import 'package:flutter/material.dart'; + +class Example extends StatelessWidget { + const Example({super.key}); + + @override + Widget build(BuildContext context) { + return const HomeScreen(); + } +} diff --git a/custom_home_drawer/lib/main.dart b/custom_home_drawer/lib/main.dart index 8282547..8d3efea 100644 --- a/custom_home_drawer/lib/main.dart +++ b/custom_home_drawer/lib/main.dart @@ -1,4 +1,4 @@ -import 'package:custom_home_drawer/screen/home_screen.dart'; +import 'package:custom_home_drawer/example.dart'; import 'package:flutter/material.dart'; void main() { @@ -15,7 +15,7 @@ class MyApp extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.blue, ), - home: const HomeScreen(), + home: const Example(), ); } } diff --git a/google_signin/lib/example.dart b/google_signin/lib/example.dart new file mode 100644 index 0000000..bbf9b52 --- /dev/null +++ b/google_signin/lib/example.dart @@ -0,0 +1,76 @@ +// Example: Sign in to Google and display the signed-in user's profile. +import 'dart:async'; + +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/material.dart'; +import 'package:google_sign_in/google_sign_in.dart'; + +import 'home.dart'; +import 'user.dart'; + +class Example extends StatefulWidget { + const Example({super.key}); + + @override + ExampleState createState() => ExampleState(); +} + +class ExampleState extends State { + final GoogleSignIn googleSignIn = GoogleSignIn.instance; + late Widget userPage; + + @override + void initState() { + super.initState(); + userPage = Home( + onSignin: _signin, + onLogout: _logout, + showLoading: false, + ); + } + + Future _signin() async { + setState(() { + userPage = Home(onSignin: null, onLogout: _logout, showLoading: true); + }); + try { + await googleSignIn.initialize(); + final GoogleSignInAccount account = await googleSignIn.authenticate(); + final GoogleSignInAuthentication auth = account.authentication; + final AuthCredential credential = GoogleAuthProvider.credential( + idToken: auth.idToken, + ); + final UserCredential authRes = + await FirebaseAuth.instance.signInWithCredential(credential); + final User? user = authRes.user; + if (user == null) return null; + + setState(() { + userPage = UserProfile(onLogout: _logout, user: user); + }); + + return user; + } catch (e) { + print(e.toString()); + return null; + } + } + + Future _logout() async { + await googleSignIn.signOut(); + setState(() { + userPage = Home( + onSignin: _signin, + onLogout: _logout, + showLoading: false, + ); + }); + + print("Logged Out"); + } + + @override + Widget build(BuildContext context) { + return userPage; + } +} diff --git a/google_signin/lib/main.dart b/google_signin/lib/main.dart index 52d4ab5..3b04dca 100644 --- a/google_signin/lib/main.dart +++ b/google_signin/lib/main.dart @@ -1,81 +1,18 @@ -import 'dart:async'; - -import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; -import 'package:google_sign_in/google_sign_in.dart'; -import 'home.dart'; -import 'user.dart'; +import 'example.dart'; void main() { - runApp(const App()); + runApp(const MyApp()); } -class App extends StatefulWidget { - const App({super.key}); - - @override - AppState createState() => AppState(); -} - -class AppState extends State { - final GoogleSignIn googleSignIn = GoogleSignIn.instance; - late Widget userPage; - - @override - void initState() { - super.initState(); - userPage = Home( - onSignin: _signin, - onLogout: _logout, - showLoading: false, - ); - } - - Future _signin() async { - setState(() { - userPage = Home(onSignin: null, onLogout: _logout, showLoading: true); - }); - try { - await googleSignIn.initialize(); - final GoogleSignInAccount account = await googleSignIn.authenticate(); - final GoogleSignInAuthentication auth = account.authentication; - final AuthCredential credential = GoogleAuthProvider.credential( - idToken: auth.idToken, - ); - final UserCredential authRes = - await FirebaseAuth.instance.signInWithCredential(credential); - final User? user = authRes.user; - if (user == null) return null; - - setState(() { - userPage = UserProfile(onLogout: _logout, user: user); - }); - - return user; - } catch (e) { - print(e.toString()); - return null; - } - } - - Future _logout() async { - await googleSignIn.signOut(); - setState(() { - userPage = Home( - onSignin: _signin, - onLogout: _logout, - showLoading: false, - ); - }); - - print("Logged Out"); - } +class MyApp extends StatelessWidget { + const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( - home: userPage, + home: const Example(), ); } } diff --git a/grid_layout/lib/example.dart b/grid_layout/lib/example.dart new file mode 100644 index 0000000..8f1c705 --- /dev/null +++ b/grid_layout/lib/example.dart @@ -0,0 +1,22 @@ +// Example: A grid layout displaying social network icons in a 2-column grid. +import 'package:flutter/material.dart'; + +import 'gridview.dart'; + +class Example extends StatelessWidget { + Example({super.key}); + + final MyGridView myGridView = MyGridView(); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + // Here we take the value from the MyHomePage object that was created by + // the App.build method, and use it to set our appbar title. + title: Text("GridView Example"), + ), + body: myGridView.build(), + ); + } +} diff --git a/grid_layout/lib/main.dart b/grid_layout/lib/main.dart index 9880e47..94ad4a6 100644 --- a/grid_layout/lib/main.dart +++ b/grid_layout/lib/main.dart @@ -1,25 +1,17 @@ import 'package:flutter/material.dart'; -import 'package:grid_layout/gridview.dart'; + +import 'example.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { - final MyGridView myGridView = MyGridView(); - - MyApp({super.key}); + const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, - home: Scaffold( - appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text("GridView Example"), - ), - body: myGridView.build(), - ), + home: Example(), ); } } diff --git a/handling_routes/lib/screens/home.dart b/handling_routes/lib/example.dart similarity index 85% rename from handling_routes/lib/screens/home.dart rename to handling_routes/lib/example.dart index 459086a..b831b84 100644 --- a/handling_routes/lib/screens/home.dart +++ b/handling_routes/lib/example.dart @@ -1,7 +1,8 @@ +// Example: A home screen with navigation to an About page via named routes. import 'package:flutter/material.dart'; -class HomePage extends StatelessWidget { - const HomePage({super.key}); +class Example extends StatelessWidget { + const Example({super.key}); @override Widget build(BuildContext context) { @@ -26,7 +27,9 @@ class HomePage extends StatelessWidget { Text( "Home Page\nClick on below icon to goto About Page", // Setting the style for the Text - style: TextStyle(fontSize: 20.0,), + style: TextStyle( + fontSize: 20.0, + ), // Set text alignment to center textAlign: TextAlign.center, ), diff --git a/handling_routes/lib/main.dart b/handling_routes/lib/main.dart index af67e80..29a084c 100644 --- a/handling_routes/lib/main.dart +++ b/handling_routes/lib/main.dart @@ -1,10 +1,11 @@ import 'package:flutter/material.dart'; import 'package:handling_routes/screens/about.dart'; -import 'package:handling_routes/screens/home.dart'; + +import 'example.dart'; void main() { runApp(MaterialApp( - home: HomePage(), // home has implicit route set at '/' + home: const Example(), // home has implicit route set at '/' // Setup routes routes: { // Set named routes diff --git a/image_editor/lib/example.dart b/image_editor/lib/example.dart new file mode 100644 index 0000000..0320904 --- /dev/null +++ b/image_editor/lib/example.dart @@ -0,0 +1,129 @@ +// Example: Image editor screen +import 'package:flutter/material.dart'; +import 'package:image_editor/ApplyFilters.dart'; +import 'package:image_editor/EditImg.dart'; +import 'package:image_editor/GetImg.dart'; +import 'package:image_editor/SaveInGallery.dart'; +import 'dart:io'; + +class Example extends StatefulWidget { + const Example({super.key}); + + @override + _ExampleState createState() => _ExampleState(); +} + +class _ExampleState extends State { + bool _selected = false; //to check if a image is selected or not + late File + _image; //here we will store the selected image and apply modifications + final double _ImageContainerHeight = 450; + final double _ImageContainerWidth = 400; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + backgroundColor: Colors.greenAccent[400], + title: Text('Flutter Image Editor'), + ), + body: Container( + child: Column( + children: [ + SizedBox( + height: _ImageContainerHeight, + width: _ImageContainerWidth, + child: _selected // checks if a image is selected or not + ? Image.file(_image) + : Image.asset('images/cam.png')), + Row( + children: [ + Spacer( + flex: 2, + ), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.greenAccent[400]), + child: Text( + 'Get_Image', // to select a image from gallery + style: TextStyle(color: Colors.white), + ), + onPressed: () async { + var Ifile = await GetiImg( + _image); // function called from GetImg.dart + if (Ifile != null) { + setState(() { + _image = Ifile; + _selected = true; + }); + } + }), + Spacer( + flex: 1, + ), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.greenAccent[400]), + child: Text( + 'Edit Image', //to start editing the shape, size, etc of the selected image + style: TextStyle(color: Colors.white), + ), + onPressed: () async { + var Ifile0 = await EditImg( + _image); // function called from EditImg.dart + if (Ifile0 != null) { + setState(() { + _image = Ifile0; + }); + } + }), + Spacer( + flex: 2, + ), + ], + ), + Row( + children: [ + Spacer( + flex: 2, + ), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.greenAccent[400]), + child: Text( + 'Apply Filters', //to start apply various photo filters to the selected image + style: TextStyle(color: Colors.white), + ), + onPressed: () async { + var Ifile0 = await ApplyFilters(context, + _image); // function called from ApplyFilters.dart + if (Ifile0 != null) { + setState(() { + _image = Ifile0; + }); + } + }), + Spacer( + flex: 1, + ), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.greenAccent[400]), + child: Text( + 'Download Editted image', //to save the edited image to gallery + style: TextStyle(color: Colors.white), + ), + onPressed: () async { + await SaveImg( + _image); // function called from SaveInGallery.dart + }), + Spacer( + flex: 2, + ), + ], + ), + ], + )), + ); + } +} diff --git a/image_editor/lib/main.dart b/image_editor/lib/main.dart index a264f5b..e0fb9e7 100644 --- a/image_editor/lib/main.dart +++ b/image_editor/lib/main.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:image_editor/HomePage.dart'; +import 'package:image_editor/example.dart'; void main() { - runApp(MyApp()); + runApp(const MyApp()); } class MyApp extends StatelessWidget { @@ -11,10 +11,8 @@ class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { - return MaterialApp( - home: HomePage() + return const MaterialApp( + home: Example(), ); } } - - diff --git a/scan_qr_code/lib/example.dart b/scan_qr_code/lib/example.dart new file mode 100644 index 0000000..da5fd98 --- /dev/null +++ b/scan_qr_code/lib/example.dart @@ -0,0 +1,80 @@ +// Example: Scan QR code +import 'package:flutter/material.dart'; +import 'package:mobile_scanner/mobile_scanner.dart'; +import 'package:scan_qr_code/utils/qr_code_scanner.dart'; +import 'package:scan_qr_code/utils/scanner_box_border_painter.dart'; + +class Example extends StatefulWidget { + const Example({super.key, this.title = 'Code Scanner Demo'}); + + final String title; + + @override + State createState() => _ExampleState(); +} + +class _ExampleState extends State { + final MobileScannerController _mobileScannerController = + MobileScannerController(); + late void Function(BarcodeCapture) _onDetect; + bool _isScanning = true; + + @override + void initState() { + super.initState(); + _onDetect = (BarcodeCapture capture) { + _mobileScannerController.stop(); + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Code info'), + content: Text( + capture.barcodes.first.rawValue?.trim() ?? 'No data found', + textAlign: TextAlign.center), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: const Text('OK')) + ], + ); + }).then((value) { + _mobileScannerController.start(); + setState(() { + _isScanning = true; + }); + }); + + setState(() { + _isScanning = false; + }); + }; + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: Text(widget.title)), + body: Center( + child: SizedBox.square( + dimension: MediaQuery.of(context).size.width - 48.0, + child: Padding( + padding: const EdgeInsets.all( + 2.5), // Required otherwise side edges hides under outside padding + child: CustomPaint( + foregroundPainter: ScannerBoxBorderPainter( + borderColor: _isScanning ? Colors.black : Colors.green), + child: Padding( + padding: const EdgeInsets.all( + 2.5), // Required otherwise scanner hides under side edges + child: QRCodeScanner( + mobileScannerController: _mobileScannerController, + onDetect: _onDetect), + ), + ), + ), + ), + ), + ); + } +} diff --git a/scan_qr_code/lib/main.dart b/scan_qr_code/lib/main.dart index 5ec97a6..57a1773 100644 --- a/scan_qr_code/lib/main.dart +++ b/scan_qr_code/lib/main.dart @@ -1,8 +1,5 @@ - import 'package:flutter/material.dart'; -import 'package:mobile_scanner/mobile_scanner.dart'; -import 'package:scan_qr_code/utils/qr_code_scanner.dart'; -import 'package:scan_qr_code/utils/scanner_box_border_painter.dart'; +import 'package:scan_qr_code/example.dart'; void main() => runApp(const MyApp()); @@ -13,82 +10,7 @@ class MyApp extends StatelessWidget { Widget build(BuildContext context) { return const MaterialApp( title: 'Code Scanner', - home: MyHomePage(title: 'Code Scanner Demo'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - final MobileScannerController _mobileScannerController = - MobileScannerController(); - late void Function(BarcodeCapture) _onDetect; - bool _isScanning = true; - - @override - void initState() { - super.initState(); - _onDetect = (BarcodeCapture capture) { - _mobileScannerController.stop(); - showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - title: const Text('Code info'), - content: Text( - capture.barcodes.first.rawValue?.trim() ?? 'No data found', - textAlign: TextAlign.center), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: const Text('OK')) - ], - ); - }).then((value) { - _mobileScannerController.start(); - setState(() { - _isScanning = true; - }); - }); - - setState(() { - _isScanning = false; - }); - }; - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: Text(widget.title)), - body: Center( - child: SizedBox.square( - dimension: MediaQuery.of(context).size.width - 48.0, - child: Padding( - padding: const EdgeInsets.all( - 2.5), // Required otherwise side edges hides under outside padding - child: CustomPaint( - foregroundPainter: ScannerBoxBorderPainter( - borderColor: _isScanning ? Colors.black : Colors.green), - child: Padding( - padding: const EdgeInsets.all( - 2.5), // Required otherwise scanner hides under side edges - child: QRCodeScanner( - mobileScannerController: _mobileScannerController, - onDetect: _onDetect), - ), - ), - ), - ), - ), + home: Example(), ); } } diff --git a/statless_counter_app/lib/example.dart b/statless_counter_app/lib/example.dart new file mode 100644 index 0000000..23bff4b --- /dev/null +++ b/statless_counter_app/lib/example.dart @@ -0,0 +1,42 @@ +// Example: Stateless widget counter using MobX +import 'package:flutter/material.dart'; +import 'package:flutter_mobx/flutter_mobx.dart'; + +import 'counter.dart'; // Import the Counter + +final counter = Counter(); // Instantiate the store + +class Example extends StatelessWidget { + const Example({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('MobX Stateless Widget Counter'), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + 'You have pushed the button this many times:', + ), + // Wrapping in the Observer will automatically re-render on changes to counter.value + Observer( + builder: (_) => Text( + '${counter.value}', + style: Theme.of(context).textTheme.headlineSmall, + ), + ), + ], + ), + ), + floatingActionButton: FloatingActionButton( + onPressed: counter.increment, + tooltip: 'Increment', + child: Icon(Icons.add), + ), + ); + } +} diff --git a/statless_counter_app/lib/main.dart b/statless_counter_app/lib/main.dart index 02c17ab..23f548c 100644 --- a/statless_counter_app/lib/main.dart +++ b/statless_counter_app/lib/main.dart @@ -1,11 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:flutter_mobx/flutter_mobx.dart'; -import 'counter.dart'; // Import the Counter +import 'example.dart'; -final counter = Counter(); // Instantiate the store - -void main() => runApp(MyApp()); +void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @@ -19,42 +16,7 @@ class MyApp extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.blue, ), - home: const MyHomePage(), + home: const Example(), ); } } - -class MyHomePage extends StatelessWidget { - const MyHomePage({super.key}); - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text('MobX Stateless Widget Counter'), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'You have pushed the button this many times:', - ), - // Wrapping in the Observer will automatically re-render on changes to counter.value - Observer( - builder: (_) => Text( - '${counter.value}', - style: Theme.of(context).textTheme.headlineSmall, - ), - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: counter.increment, - tooltip: 'Increment', - child: Icon(Icons.add), - ), - ); - } -} \ No newline at end of file diff --git a/using_bottom_nav_bar/lib/example.dart b/using_bottom_nav_bar/lib/example.dart new file mode 100644 index 0000000..16a7ef8 --- /dev/null +++ b/using_bottom_nav_bar/lib/example.dart @@ -0,0 +1,75 @@ +// Example: Using bottom navigation bar with tabs +import 'package:flutter/material.dart'; +import 'package:using_bottom_nav_bar/tabs/first.dart'; +import 'package:using_bottom_nav_bar/tabs/second.dart'; +import 'package:using_bottom_nav_bar/tabs/third.dart'; + +class Example extends StatefulWidget { + const Example({super.key}); + + @override + ExampleState createState() => ExampleState(); +} + +// SingleTickerProviderStateMixin is used for animation +class ExampleState extends State with SingleTickerProviderStateMixin { + // Create a tab controller + late TabController controller; + + @override + void initState() { + super.initState(); + + // Initialize the Tab Controller + controller = TabController(length: 3, vsync: this); + } + + @override + void dispose() { + // Dispose of the Tab Controller + controller.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + // Appbar + appBar: AppBar( + // Title + title: Text("Using Bottom Navigation Bar"), + // Set the background color of the App Bar + backgroundColor: Colors.blue, + ), + // Set the TabBar view as the body of the Scaffold + body: TabBarView( + // Add tabs as widgets + controller: controller, + // Add tabs as widgets + children: [FirstTab(), SecondTab(), ThirdTab()], + ), + // Set the bottom navigation bar + bottomNavigationBar: Material( + // set the color of the bottom navigation bar + color: Colors.blue, + // set the tab bar as the child of bottom navigation bar + child: TabBar( + tabs: [ + Tab( + // set icon to the tab + icon: Icon(Icons.favorite), + ), + Tab( + icon: Icon(Icons.adb), + ), + Tab( + icon: Icon(Icons.airport_shuttle), + ), + ], + // setup the controller + controller: controller, + ), + ), + ); + } +} diff --git a/using_bottom_nav_bar/lib/main.dart b/using_bottom_nav_bar/lib/main.dart index ff6991b..7cc6cc5 100644 --- a/using_bottom_nav_bar/lib/main.dart +++ b/using_bottom_nav_bar/lib/main.dart @@ -1,82 +1,20 @@ import 'package:flutter/material.dart'; -import 'package:using_bottom_nav_bar/tabs/first.dart'; -import 'package:using_bottom_nav_bar/tabs/second.dart'; -import 'package:using_bottom_nav_bar/tabs/third.dart'; +import 'package:using_bottom_nav_bar/example.dart'; void main() { - runApp(MaterialApp( - // Title - title: "Using Tabs", - // Home - home: MyHome())); + runApp(const MyApp()); } -class MyHome extends StatefulWidget { - const MyHome({super.key}); - - @override - MyHomeState createState() => MyHomeState(); -} - -// SingleTickerProviderStateMixin is used for animation -class MyHomeState extends State with SingleTickerProviderStateMixin { - // Create a tab controller - late TabController controller; - - @override - void initState() { - super.initState(); - - // Initialize the Tab Controller - controller = TabController(length: 3, vsync: this); - } - - @override - void dispose() { - // Dispose of the Tab Controller - controller.dispose(); - super.dispose(); - } +class MyApp extends StatelessWidget { + const MyApp({super.key}); @override Widget build(BuildContext context) { - return Scaffold( - // Appbar - appBar: AppBar( - // Title - title: Text("Using Bottom Navigation Bar"), - // Set the background color of the App Bar - backgroundColor: Colors.blue, - ), - // Set the TabBar view as the body of the Scaffold - body: TabBarView( - // Add tabs as widgets - controller: controller, - // Add tabs as widgets - children: [FirstTab(), SecondTab(), ThirdTab()], - ), - // Set the bottom navigation bar - bottomNavigationBar: Material( - // set the color of the bottom navigation bar - color: Colors.blue, - // set the tab bar as the child of bottom navigation bar - child: TabBar( - tabs: [ - Tab( - // set icon to the tab - icon: Icon(Icons.favorite), - ), - Tab( - icon: Icon(Icons.adb), - ), - Tab( - icon: Icon(Icons.airport_shuttle), - ), - ], - // setup the controller - controller: controller, - ), - ), + return MaterialApp( + // Title + title: "Using Tabs", + // Home + home: const Example(), ); } } diff --git a/using_custom_fonts/lib/example.dart b/using_custom_fonts/lib/example.dart new file mode 100644 index 0000000..d646bd7 --- /dev/null +++ b/using_custom_fonts/lib/example.dart @@ -0,0 +1,31 @@ +// Example: Displays text using a custom font loaded from the app's assets. +import 'package:flutter/material.dart'; + +import './utils.dart' as utils; + +class Example extends StatelessWidget { + const Example({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + // Appbar + appBar: AppBar( + // Title + title: Text("Using Custom Fonts"), + ), + // Body + body: Container( + // Center the content + child: Center( + // Add Text + child: Text("The quick brown fox jumps over the lazy dog", + // Center align text + textAlign: TextAlign.center, + // set a text style which defines a custom font + style: utils.getCustomFontTextStyle()), + ), + ), + ); + } +} diff --git a/using_custom_fonts/lib/main.dart b/using_custom_fonts/lib/main.dart index 5fe3a97..ed7632e 100644 --- a/using_custom_fonts/lib/main.dart +++ b/using_custom_fonts/lib/main.dart @@ -1,29 +1,12 @@ import 'package:flutter/material.dart'; -import './utils.dart' as utils; +import 'example.dart'; void main() { runApp(MaterialApp( - // Title - title: "Using Custom Fonts", - // Home - home: Scaffold( - // Appbar - appBar: AppBar( - // Title - title: Text("Using Custom Fonts"), - ), - // Body - body: Container( - // Center the content - child: Center( - // Add Text - child: Text("The quick brown fox jumps over the lazy dog", - // Center align text - textAlign: TextAlign.center, - // set a text style which defines a custom font - style: utils.getCustomFontTextStyle()), - ), - ), - ))); + // Title + title: "Using Custom Fonts", + // Home + home: const Example(), + )); } diff --git a/using_listview/lib/example.dart b/using_listview/lib/example.dart new file mode 100644 index 0000000..c3f5272 --- /dev/null +++ b/using_listview/lib/example.dart @@ -0,0 +1,17 @@ +// Example: A scrollable list of contacts with avatar, name, and email. +import 'package:flutter/material.dart'; +import 'package:using_listview/contact_page.dart'; + +class Example extends StatelessWidget { + const Example({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("Using Listview"), + ), + body: const ContactPage(), + ); + } +} diff --git a/using_listview/lib/main.dart b/using_listview/lib/main.dart index dd7fd17..f7d2a9d 100644 --- a/using_listview/lib/main.dart +++ b/using_listview/lib/main.dart @@ -1,14 +1,10 @@ import 'package:flutter/material.dart'; -import 'package:using_listview/contact_page.dart'; + +import 'example.dart'; void main() { runApp(MaterialApp( debugShowCheckedModeBanner: false, - home: Scaffold( - appBar: AppBar( - title: Text("Using Listview"), - ), - body: ContactPage(), - ), + home: const Example(), )); } diff --git a/using_listwheelscrollview/lib/example.dart b/using_listwheelscrollview/lib/example.dart new file mode 100644 index 0000000..9a82efd --- /dev/null +++ b/using_listwheelscrollview/lib/example.dart @@ -0,0 +1,12 @@ +// Example: List wheel scroll view +import 'package:flutter/material.dart'; +import 'listwheel.dart'; + +class Example extends StatelessWidget { + const Example({super.key}); + + @override + Widget build(BuildContext context) { + return const Listwheel(); + } +} diff --git a/using_listwheelscrollview/lib/main.dart b/using_listwheelscrollview/lib/main.dart index 527434d..6b925c8 100644 --- a/using_listwheelscrollview/lib/main.dart +++ b/using_listwheelscrollview/lib/main.dart @@ -1,21 +1,21 @@ -import 'package:flutter/material.dart'; -import 'listwheel.dart'; - -void main() { - runApp(MyApp()); -} - -class MyApp extends StatelessWidget { - const MyApp({super.key}); - - // This widget is the root of your application. - @override - Widget build(BuildContext context) { - return MaterialApp( - theme: ThemeData.dark().copyWith( - scaffoldBackgroundColor: Color(0XFF0A0E21), - ), - home: Listwheel(), - ); - } -} +import 'package:flutter/material.dart'; +import 'example.dart'; + +void main() { + runApp(const MyApp()); +} + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + // This widget is the root of your application. + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData.dark().copyWith( + scaffoldBackgroundColor: Color(0XFF0A0E21), + ), + home: const Example(), + ); + } +} diff --git a/using_platform_adaptive/lib/example.dart b/using_platform_adaptive/lib/example.dart new file mode 100644 index 0000000..262208f --- /dev/null +++ b/using_platform_adaptive/lib/example.dart @@ -0,0 +1,123 @@ +// Example: Platform adaptive widgets +import 'package:flutter/material.dart'; +import 'package:using_platform_adaptive/common_widgets/adaptive_button.dart'; +import 'package:using_platform_adaptive/common_widgets/adaptive_date_picker.dart'; +import 'package:using_platform_adaptive/common_widgets/adaptive_indicator.dart'; + +class Example extends StatefulWidget { + const Example({super.key, this.title = 'Using Platform Adaptive'}); + final String title; + + @override + State createState() => _ExampleState(); +} + +class _ExampleState extends State { + TargetPlatform? selectedPlatform; + final String introText = "Flutter's flagship feature is it's ability to write" + " code once and have it run on multiple devices. While this is great, sometimes" + " you want the user interface to look more native to its platform. This project" + " showcases the Platform Adaptive pattern, a powerful pattern that allows you" + " to create widgets that you write once and look natural on any device. The" + " demo is just set up to work on android and iOS but can be extended to work" + " on web, native and any other platform flutter supports."; + + @override + Widget build(BuildContext context) { + final platforms = { + "Default": null, + "Android": TargetPlatform.android, + "iOS": TargetPlatform.iOS, + }; + return Scaffold( + backgroundColor: Colors.white, + appBar: AppBar( + title: Text(widget.title), + ), + body: Container( + padding: const EdgeInsets.all(30), + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Text(introText), + const SizedBox(height: 30), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("Force a platform: "), + DropdownButton( + value: selectedPlatform, + items: List.from(platforms.entries.map( + (e) => DropdownMenuItem( + value: e.value, child: Text(e.key)))), + onChanged: (value) { + setState(() { + selectedPlatform = value; + }); + }, + ), + ], + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("Buttons: "), + AdaptiveButton( + forcePlatform: selectedPlatform, + color: Colors.teal, + onPressed: () {}, + child: const Text("I'm a button"), + ), + ], + ), + const SizedBox(height: 10), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("Dialogs: "), + AdaptiveButton( + forcePlatform: selectedPlatform, + color: Colors.teal, + onPressed: () { + showDialog( + context: context, + builder: (context) { + return AdaptiveDialog( + title: "Test", + content: const Text("This is the content area"), + actions: [ + AdaptiveDialogAction( + text: "Action 1", + forcePlatform: selectedPlatform, + onPressed: () {}, + ), + AdaptiveDialogAction( + text: "Action 2", + forcePlatform: selectedPlatform, + onPressed: () {}, + ), + ], + forcePlatform: selectedPlatform, + ); + }); + }, + child: const Text("Show"), + ), + ], + ), + const SizedBox(height: 10), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("Loading Indicator: "), + AdaptiveIndicator( + forcePlatform: selectedPlatform, + ), + ], + ), + ], + ), + ), + ); + } +} diff --git a/using_platform_adaptive/lib/main.dart b/using_platform_adaptive/lib/main.dart index fed9164..f66edfe 100644 --- a/using_platform_adaptive/lib/main.dart +++ b/using_platform_adaptive/lib/main.dart @@ -1,7 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:using_platform_adaptive/common_widgets/adaptive_button.dart'; -import 'package:using_platform_adaptive/common_widgets/adaptive_date_picker.dart'; -import 'package:using_platform_adaptive/common_widgets/adaptive_indicator.dart'; +import 'package:using_platform_adaptive/example.dart'; void main() { runApp(const MyApp()); @@ -18,125 +16,7 @@ class MyApp extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.teal, ), - home: const MyHomePage(title: 'Using Platform Adaptive'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - TargetPlatform? selectedPlatform; - final String introText = "Flutter's flagship feature is it's ability to write" - " code once and have it run on multiple devices. While this is great, sometimes" - " you want the user interface to look more native to its platform. This project" - " showcases the Platform Adaptive pattern, a powerful pattern that allows you" - " to create widgets that you write once and look natural on any device. The" - " demo is just set up to work on android and iOS but can be extended to work" - " on web, native and any other platform flutter supports."; - - @override - Widget build(BuildContext context) { - final platforms = { - "Default": null, - "Android": TargetPlatform.android, - "iOS": TargetPlatform.iOS, - }; - return Scaffold( - backgroundColor: Colors.white, - appBar: AppBar( - title: Text(widget.title), - ), - body: Container( - padding: const EdgeInsets.all(30), - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Text(introText), - const SizedBox(height: 30), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text("Force a platform: "), - DropdownButton( - value: selectedPlatform, - items: List.from(platforms.entries.map( - (e) => DropdownMenuItem( - value: e.value, child: Text(e.key)))), - onChanged: (value) { - setState(() { - selectedPlatform = value; - }); - }, - ), - ], - ), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text("Buttons: "), - AdaptiveButton( - forcePlatform: selectedPlatform, - color: Colors.teal, - onPressed: () {}, - child: const Text("I'm a button"), - ), - ], - ), - const SizedBox(height: 10), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text("Dialogs: "), - AdaptiveButton( - forcePlatform: selectedPlatform, - color: Colors.teal, - onPressed: () { - showDialog( - context: context, - builder: (context) { - return AdaptiveDialog( - title: "Test", - content: const Text("This is the content area"), - actions: [ - AdaptiveDialogAction( - text: "Action 1", - forcePlatform: selectedPlatform, - onPressed: () {}, - ), - AdaptiveDialogAction( - text: "Action 2", - forcePlatform: selectedPlatform, - onPressed: () {}, - ), - ], - forcePlatform: selectedPlatform, - ); - }); - }, - child: const Text("Show"), - ), - ], - ), - const SizedBox(height: 10), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text("Loading Indicator: "), - AdaptiveIndicator( - forcePlatform: selectedPlatform, - ), - ], - ), - ], - ), - ), + home: const Example(), ); } } diff --git a/view_pdf_file/lib/example.dart b/view_pdf_file/lib/example.dart new file mode 100644 index 0000000..acf43d8 --- /dev/null +++ b/view_pdf_file/lib/example.dart @@ -0,0 +1,84 @@ +// Example: View PDF file +import 'package:flutter/material.dart'; +import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; +import 'package:view_pdf_file/constants.dart'; +import 'package:view_pdf_file/viewPDF.dart'; + +class Example extends StatefulWidget { + const Example({super.key}); + + @override + ExampleState createState() => ExampleState(); +} + +class ExampleState extends State { + bool isLoading = false; + late Widget pdfViewer; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("View PDF File"), + ), + body: Container( + child: Center( + child: isLoading + ? CircularProgressIndicator() + : Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + mainAxisSize: MainAxisSize.max, + children: [ + ElevatedButton( + onPressed: () { + loadFromAsset(); + }, + child: Text("Load local PDF"), + ), + ElevatedButton( + onPressed: () { + loadFromURL(); + }, + child: Text("Load PDF via URL"), + ), + ], + ), + ), + ), + ); + } + + Future loadFromAsset() async { + setState(() { + isLoading = true; + }); + pdfViewer = SfPdfViewer.asset('assets/Hello.pdf'); + setState(() { + isLoading = false; + }); + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ViewPDF(viewer: pdfViewer), + ), + ); + } + + Future loadFromURL() async { + setState(() { + isLoading = true; + }); + + pdfViewer = SfPdfViewer.network(Constants.pdfURL); + setState(() { + isLoading = false; + }); + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ViewPDF(viewer: pdfViewer), + ), + ); + } +} diff --git a/view_pdf_file/lib/main.dart b/view_pdf_file/lib/main.dart index 46befc4..ee0f546 100644 --- a/view_pdf_file/lib/main.dart +++ b/view_pdf_file/lib/main.dart @@ -1,10 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; -import 'package:view_pdf_file/constants.dart'; -import 'package:view_pdf_file/viewPDF.dart'; +import 'package:view_pdf_file/example.dart'; void main() { - runApp(MyApp()); + runApp(const MyApp()); } class MyApp extends StatelessWidget { @@ -18,85 +16,6 @@ class MyApp extends StatelessWidget { primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), - home: HomePage()); - } -} - -class HomePage extends StatefulWidget { - const HomePage({super.key}); - - @override - _HomePageState createState() => _HomePageState(); -} - -class _HomePageState extends State { - bool isLoading = false; - late Widget pdfViewer; - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text("View PDF File"), - ), - body: Container( - child: Center( - child: isLoading - ? CircularProgressIndicator() - : Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - mainAxisSize: MainAxisSize.max, - children: [ - ElevatedButton( - onPressed: () { - loadFromAsset(); - }, - child: Text("Load local PDF"), - ), - ElevatedButton( - onPressed: () { - loadFromURL(); - }, - child: Text("Load PDF via URL"), - ), - ], - ), - ), - ), - ); - } - - Future loadFromAsset() async { - setState(() { - isLoading = true; - }); - pdfViewer = SfPdfViewer.asset('assets/Hello.pdf'); - setState(() { - isLoading = false; - }); - Navigator.push( - context, - MaterialPageRoute( - builder: (_) => ViewPDF(viewer: pdfViewer), - ), - ); - } - - Future loadFromURL() async { - setState(() { - isLoading = true; - }); - - pdfViewer = SfPdfViewer.network(Constants.pdfURL); - setState(() { - isLoading = false; - }); - Navigator.push( - context, - MaterialPageRoute( - builder: (_) => ViewPDF(viewer: pdfViewer), - ), - ); + home: const Example()); } }