Add environment variables
parent
b39bd90b1f
commit
5811c5315f
|
@ -0,0 +1 @@
|
|||
SERVER_HOST=http://10.0.2.2:8000
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:together_mobile/utils/env.dart';
|
||||
|
||||
import '/common/theme.dart';
|
||||
import '/database/hive_database.dart';
|
||||
|
@ -11,6 +12,7 @@ import 'notification_api.dart';
|
|||
final easyLoading = EasyLoading.init();
|
||||
|
||||
void main() async {
|
||||
await EnvConfig.loadEnv(Env.product);
|
||||
initGetIt();
|
||||
await NotificationAPI.init();
|
||||
HiveDatabase.registerAdapter();
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:together_mobile/utils/env.dart';
|
||||
|
||||
import '/common/theme.dart';
|
||||
import '/database/hive_database.dart';
|
||||
import '/router/router.dart';
|
||||
import '/models/init_get_it.dart';
|
||||
import 'notification_api.dart';
|
||||
|
||||
final easyLoading = EasyLoading.init();
|
||||
|
||||
void main() async {
|
||||
await EnvConfig.loadEnv(Env.development);
|
||||
initGetIt();
|
||||
await NotificationAPI.init();
|
||||
HiveDatabase.registerAdapter();
|
||||
runApp(const Together());
|
||||
}
|
||||
|
||||
class Together extends StatelessWidget {
|
||||
const Together({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: "Together",
|
||||
theme: lightThemeData(context),
|
||||
darkTheme: darkThemeData(context),
|
||||
routerConfig: router,
|
||||
// This builder is used to dismiss keyboard while tap anywhere of the
|
||||
// screen excluding the input widgets.
|
||||
builder: (context, child) {
|
||||
child = easyLoading(context, child);
|
||||
child = GestureDetector(
|
||||
onTap: () {
|
||||
if (FocusManager.instance.primaryFocus != null) {
|
||||
FocusManager.instance.primaryFocus!.unfocus();
|
||||
}
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
return child;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import '/common/theme.dart';
|
||||
import '/database/hive_database.dart';
|
||||
import '/router/router.dart';
|
||||
import '/models/init_get_it.dart';
|
||||
import '/utils/env.dart';
|
||||
import 'notification_api.dart';
|
||||
|
||||
final easyLoading = EasyLoading.init();
|
||||
|
||||
// 本地开发环境
|
||||
|
||||
void main() async {
|
||||
await EnvConfig.loadEnv(Env.local);
|
||||
initGetIt();
|
||||
await NotificationAPI.init();
|
||||
HiveDatabase.registerAdapter();
|
||||
runApp(const Together());
|
||||
}
|
||||
|
||||
class Together extends StatelessWidget {
|
||||
const Together({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: "Together",
|
||||
theme: lightThemeData(context),
|
||||
darkTheme: darkThemeData(context),
|
||||
routerConfig: router,
|
||||
// This builder is used to dismiss keyboard while tap anywhere of the
|
||||
// screen excluding the input widgets.
|
||||
builder: (context, child) {
|
||||
child = easyLoading(context, child);
|
||||
child = GestureDetector(
|
||||
onTap: () {
|
||||
if (FocusManager.instance.primaryFocus != null) {
|
||||
FocusManager.instance.primaryFocus!.unfocus();
|
||||
}
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
return child;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,14 +1,15 @@
|
|||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
import '/models/init_get_it.dart';
|
||||
import '/models/token_model.dart';
|
||||
|
||||
const baseUrl = 'http://10.0.2.2:8000/api';
|
||||
const userAvatarsUrl = 'http://10.0.2.2:8000/static/avatars/user';
|
||||
const groupChatAvatarsUrl = 'http://10.0.2.2:8000/static/avatars/group_chat';
|
||||
final baseUrl = dotenv.env['SERVER_HOST'];
|
||||
final userAvatarsUrl = '$baseUrl/static/avatars/user';
|
||||
final groupChatAvatarsUrl = '$baseUrl/static/avatars/group_chat';
|
||||
|
||||
final baseOptions = BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
baseUrl: baseUrl!,
|
||||
connectTimeout: const Duration(seconds: 5),
|
||||
receiveTimeout: const Duration(seconds: 5),
|
||||
);
|
||||
|
@ -16,10 +17,10 @@ final baseOptions = BaseOptions(
|
|||
var request = Dio(baseOptions)
|
||||
..interceptors.add(
|
||||
InterceptorsWrapper(
|
||||
onRequest:
|
||||
(RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
String baseUrl = options.baseUrl.replaceFirst('/api', '');
|
||||
options.baseUrl = baseUrl;
|
||||
onRequest: (
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
if (getIt.get<Token>().token != '') {
|
||||
options.headers['Authorization'] =
|
||||
'Bearer ${getIt.get<Token>().token}';
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
enum Env {
|
||||
product,
|
||||
development,
|
||||
local,
|
||||
}
|
||||
|
||||
class EnvConfig {
|
||||
static late Env env;
|
||||
|
||||
static Future<void> loadEnv(Env e) async {
|
||||
switch (e) {
|
||||
case Env.local:
|
||||
env = Env.local;
|
||||
await dotenv.load(fileName: '.local.env');
|
||||
break;
|
||||
case Env.development:
|
||||
env = Env.development;
|
||||
await dotenv.load(fileName: '.dev.env');
|
||||
break;
|
||||
case Env.product:
|
||||
await dotenv.load(fileName: '.prod.env');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -390,6 +390,14 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.3"
|
||||
flutter_dotenv:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_dotenv
|
||||
sha256: "9357883bdd153ab78cbf9ffa07656e336b8bbb2b5a3ca596b0b27e119f7c7d77"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.1.0"
|
||||
flutter_easyloading:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
|
@ -39,6 +39,7 @@ dependencies:
|
|||
fast_rsa: ^3.5.7
|
||||
flutter:
|
||||
sdk: flutter
|
||||
flutter_dotenv: ^5.1.0
|
||||
flutter_easyloading: ^3.0.5
|
||||
flutter_local_notifications: ^17.0.0
|
||||
flutter_pickers: ^2.1.9
|
||||
|
@ -84,6 +85,8 @@ flutter:
|
|||
assets:
|
||||
- assets/images/
|
||||
- assets/icons/
|
||||
- .local.env
|
||||
- .dev.env
|
||||
# An image asset can refer to one or more resolution-specific "variants", see
|
||||
# https://flutter.dev/assets-and-images/#resolution-aware
|
||||
# For details regarding adding assets from package dependencies, see
|
||||
|
|
Loading…
Reference in New Issue