mahg_essential_package 0.0.5 copy "mahg_essential_package: ^0.0.5" to clipboard
mahg_essential_package: ^0.0.5 copied to clipboard

A simple mahg essential package.

example/lib/main.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:mahg_essential_package/mahg_essential_package.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ConfigService.init();
  AppLocalizations.data = {
    'Login': 'تسجيل الدخول',
    'Location': 'الموقع',
    'Phone Number': 'رقم الهاتف'
  };
  ConfigService.apiURL = 'https://jsonplaceholder.typicode.com/';
  BeamerService.routesEx = {
    '/': (context, state, data) {
      return BeamerService.beamPageEx(
        key: ValueKey('home'),
        title: 'Home',
        child: MyHomePage(
          title: 'test',
        ),
      );
    },
    '/person': (context, state, data) {
      return BeamerService.beamPageEx(
        key: ValueKey('person'),
        title: 'Person',
        child: PersonPage(),
      );
    },
  };
  runApp(RestartWidget(MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Demo',
      supportedLocales: [
        Locale('en', 'US'),
        Locale('ar', 'SA'),
      ],
      locale: Locale('ar', 'SA'),
      localeResolutionCallback: (locale, supportedLocales) {
        print('Check Language');
        print(locale?.languageCode);

        var lang = ConfigService.getValueString('language');
        if (ConfigService.getValueString('language') == '') {
          for (Locale supportedLocale in supportedLocales) {
            if (supportedLocale.languageCode == locale?.languageCode ||
                supportedLocale.countryCode == locale?.countryCode) {
              ConfigService.setValueString(
                  'language', supportedLocale.languageCode);
              lang = supportedLocale.languageCode;
              print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx${lang}');
              return supportedLocale;
            }
          }
        } else {
          print(
              'else xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx${ConfigService.getValueString('language')}');
          var local = supportedLocales
              .where((element) => element.languageCode == lang)
              .first;
          return local;
        }
      },
      localizationsDelegates: const [
        GlobalCupertinoLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        AppLocalizations.delegate,

        // Built-in localization of basic text for Material widgets
        GlobalMaterialLocalizations.delegate,
        // Built-in localization for text direction LTR/RTL
        GlobalWidgetsLocalizations.delegate,
      ],
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routerDelegate: BeamerService.routerDelegate,
      routeInformationParser: BeamerService.getBeamerParser,
      backButtonDispatcher: BeamerService.getBeamerBackButtonDispatcher,
      scrollBehavior: AppScrollBehavior(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              AppLocalizations.of(context)!.translate('Login'),
              style: TextStyle(fontSize: 25),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 10),
            Text(
              AppLocalizations.of(context)!.translate('Location'),
              style: TextStyle(fontSize: 25),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 10),
            Text(
              AppLocalizations.of(context)!.translate('Phone Number'),
              style: TextStyle(fontSize: 25),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 10),
            Text(
              'This will not be translated.',
              style: TextStyle(fontSize: 25),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 10),
            ElevatedButton(
                onPressed: () async {
                  var x = await ConfigService.setValueString(
                      'language',
                      ConfigService.getValueString('language') == 'ar'
                          ? 'en'
                          : 'ar');
                  print('aaaaaaaaaaaaaaaaaa${x}');
                  RestartWidget.restartApp(context);
                },
                child: Text('test')),
            SizedBox(height: 10),
            ElevatedButton(
                onPressed: () async {
                  BeamerService.push(context: context, route: 'person');
                },
                child: Text('Go To Person Page')),
            SizedBox(height: 10),
            ElevatedButton(
                onPressed: () async {
                  LoadingOverlay.showFutureLoadingDialog(
                      backLabel: 'back',
                      barrierDismissible: true,
                      context: context,
                      future: () => Future.delayed(
                          Duration(seconds: 3), () => {print('hello')}));
                },
                child: Text('Test Loading')),
          ],
        ),
      ),
    );
  }
}

class PersonController extends MahgController {
  List<dynamic>? usersList;

  @override
  void init(Widget page) {
    // TODO: implement init
  }

  @override
  Future initLate(Widget page) async {
    if (page is PersonPage) {
      await getList();
    }
  }

  Future<void> getList() async {
    var result = await ApiService().httpGetDynamic('users');
    if (result.success) {
      print('==============================================${result.data}');
      usersList = result.data;
    } else {
      usersList = [];
    }
  }
}

class PersonPage extends MahgStatefulWidget<PersonController> {
  const PersonPage({PersonController? controllerEx, super.key})
      : super(controllerEx);

  @override
  State<PersonPage> createState() => _PersonPageState();
}

class _PersonPageState extends MahgState<PersonPage, PersonController> {
  @override
  PersonController createController() {
    return widget.controller ?? PersonController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: (controller.usersList == null ||
                (controller.usersList?.isEmpty ?? false))
            ? Center(child: Text('No Data Found'))
            : Center(child: Text('Data here')));
  }
}