tcmpp_flutter 2.0.7 copy "tcmpp_flutter: ^2.0.7" to clipboard
tcmpp_flutter: ^2.0.7 copied to clipboard

TCMPP Flutter SDK.

example/lib/main.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'login/login_page.dart';
import 'home/home_page.dart';
import 'login/login_service.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:tcmpp_flutter_example/app_localizations.dart';
export 'main.dart' show _MyAppState;
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp(await navigateToInitialPage()));
}

class MyApp extends StatefulWidget {
  final Widget initialPage;

  MyApp(this.initialPage);

  @override
  MyAppState createState() => MyAppState(initialPage);
}

class MyAppState extends State<MyApp> {
  final Widget initialPage;
  final ValueNotifier<Locale> _localeNotifier = ValueNotifier(Locale('en', ''));

  Locale _currentLocale = Locale('en', '');

  MyAppState(this.initialPage);
  void changeLanguage(Locale newLocale) {
    _localeNotifier.value = newLocale;
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    return ValueListenableBuilder<Locale>(
      valueListenable: _localeNotifier,
      builder: (context, locale, child) {
        _currentLocale = locale;
        return MaterialApp(
          locale: _currentLocale,
          home: initialPage,
          routes: {
            '/home': (context) => HomePage(key: UniqueKey()),
            '/login': (context) => LoginPage(key: UniqueKey()),
          },
          localizationsDelegates: [
            AppLocalizationsDelegate(),
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('en', ''),
            const Locale('zh', 'CN'),
            const Locale('fr', ''),
            const Locale('id', ''),
          ],
        );
      },
    );
  }
}

Future<Widget> navigateToInitialPage() async {
  String? userAccount = await LoginService.getUserToken();
  if (userAccount != null && userAccount.isNotEmpty) {
    return FutureBuilder<bool>(
      future: LoginService.login(userAccount),
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return HomePage(key: UniqueKey());
        } else if (snapshot.hasError) {
          return LoginPage(key: UniqueKey());
        } else {
          return HomePage(key: UniqueKey());
        }
      },
    );
  } else {
    return LoginPage(key: UniqueKey());
  }
}