Flutter插件dimigoin_flutter_plugin的使用_这是用于开发与迪米高相关的应用程序的迪米高人 Flutter 插件
Flutter特定功能集成插件dimigoin_flutter_plugin的使用
Flutter插件dimigoin_flutter_plugin
这是用于开发与迪米高相关的应用程序的迪米高人 Flutter 插件。
Getting Started
Flutter插件dimigoin_flutter_plugin依赖性
首先,在 pubspec.yaml
文件中添加以下代码以将此插件添加到项目中:
dependencies:
dimigoin_flutter_plugin: ^1.0.3
该插件使用了以下列表中的包。在开发过程中请注意这些包:
- dio (4.0.4)
- flutter_secure_storage (5.0.2)
- socket_io_client (^2.0.0-beta.4-nullsafety.0) - 为了与 Socket.io 服务器版本兼容,插件使用了预发布版本。
Flutter插件dimigoin_flutter_plugin项目前置工作
该插件使用了 flutter_secure_storage
包,因此需要设置最低 Android 版本。请修改 android/app/build.gradle
文件中的 minSdkVersion
为 18 或更高版本:
android {
..
defaultConfig {
..
minSdkVersion 18
..
}
}
实现指南
首先,为了使用插件,需要在应用程序启动时初始化插件。在 main.dart
文件中添加以下代码:
import 'package:dimigoin_flutter_plugin/dimigoin_flutter_plugin.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DimigoinFlutterPlugin().initializeApp(dimigoStudentAPIAuthToken: "ZGltaWdvaW46ZGtd0bXNqZiNAIQ=");
runApp(const MyApp());
}
迪米高人账号登录实现代码
import 'package:dimigoin_flutter_plugin/dimigoin_flutter_plugin.dart';
// 创建迪米高人登录对象
DimigoinAccount _dimigoinAccount = DimigoinAccount();
// 获取当前已登录的用户信息
DimigoinUser currentUser = _dimigoinAccount.currentUser;
// 当前用户信息变更时发送的 Stream
Stream<DimigoinUser?> userChangeStream = _dimigoinAccount.userChangeStream;
// 获取服务器上存储的账户数据
await _dimigoinAccount.fetchAccountData();
// 登录账户
bool isLoginSuccess = await _dimigoinAccount.login(userId, userPassword);
// 登出账户
bool isLogoutSuccess = await _dimigoinAccount.logout();
// 使用 RefreshToken 刷新 AccessToken
bool isTokenRefreshSuccess = await _dimigoinAccount.refreshAccessToken();
// 从本地存储加载 AccessToken
await _dimigoinAccount.loadSavedToken();
// 检查本地存储的 AccessToken 是否有效
bool isValidateAccessToken = await _dimigoinAccount.validateAccessToken();
// 检查当前是否已登录
bool isNowLogin = await _dimigoinAccount.checkNowLogin();
获取迪米高人提供的餐食信息代码
import 'package:dimigoin_flutter_plugin/dimigoin_flutter_plugin.dart';
// 创建迪米高人餐食对象
DimigoinMeal _dimigoinMeal = DimigoinMeal();
// 获取一周的餐食信息
List weeklyMeal = await _dimigoinMeal.getWeeklyMeal();
// 获取一天的餐食信息
Map dailyMeal = await _dimigoinMeal.getDailyMeal(getTodayInfo, date); // 如果 getTodayInfo 为 true,则不需要指定 date。
使用 Dalgeurak 服务相关 API 的代码
import 'package:dimigoin_flutter_plugin/dimigoin_flutter_plugin.dart';
// 创建 Dalgeurak 服务对象
DalgeurakService _dalgeurakService = DalgeurakService();
// 学生进行自助签到
Map result = await _dalgeurakService.mealCheckInWithJWT(jwtToken);
// 管理员进行签到
Map result = await _dalgeurakService.mealCheckInByManager(studentId, studentName);
// 学生申请提前或延后就餐
Map result = await _dalgeurakService.setUserMealException(exceptionType, reason);
// 获取学生当前的签到状态和提前/延后就餐状态
Map result = await _dalgeurakService.getUserMealInfo();
// 获取所有年级的就餐顺序
Map result = await _dalgeurakService.getMealSequence();
// 设置各年级的就餐顺序
Map result = await _dalgeurakService.setMealSequence(grade, mealType, sequence);
// 获取所有年级的就餐时间
Map result = await _dalgeurakService.getMealTime();
// 设置各年级的就餐时间
Map result = await _dalgeurakService.setMealTime(grade, mealType, time);
// 如果午餐队伍延迟,调整各年级的就餐时间
Map result = await _dalgeurakService.setMealExtraTime();
Author
示例代码
import 'package:flutter/material.dart';
import 'package:dimigoin_flutter_plugin/dimigoin_flutter_plugin.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DimigoinFlutterPlugin().initializeApp(dimigoStudentAPIAuthToken: "ZGltaWdvaW46ZGtd0bXNqZiNAIQ=");
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Plugin Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: '迪米高人 Flutter 插件示例'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DimigoinAccount _dimigoinAccount = DimigoinAccount();
DimigoinMeal _dimigoinMeal = DimigoinMeal();
DimigoinTimetable _dimigoinTimetable = DimigoinTimetable();
DalgeurakService _dalgeurakService = DalgeurakService();
late double _height, _width;
[@override](/user/override)
Widget build(BuildContext context) {
_height = MediaQuery.of(context).size.height;
_width = MediaQuery.of(context).size.width;
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
StreamBuilder(
stream: _dimigoinAccount.userChangeStream,
builder: (context, snapshot) => Text("当前登录的用户名: ${(snapshot.data as DimigoinUser?)?.name.toString()}")
),
StreamBuilder(
stream: _dalgeurakService.studentMealStatusStream,
builder: (context, snapshot) => Text("全学年餐食状态变更 Stream 输入: ${snapshot.data.toString()}")
),
SizedBox(height: _height * 0.075),
SizedBox(
height: _height * 0.5,
child: ListView(
children: <Widget>[
ListTile(
leading: Icon(Icons.login),
title: Text('登录'),
onTap: () => _showLoginDialog(context),
),
ListTile(
leading: Icon(Icons.logout),
title: Text('登出'),
onTap: () {
_dimigoinAccount.logout();
_showToast("已登出");
},
),
ListTile(
leading: Icon(Icons.person),
title: Text('登录后,查询用户相关信息'),
onTap: () async => _showToast((_dimigoinAccount.currentUser.toJson()).toString()),
),
ListTile(
leading: Icon(Icons.fastfood_rounded),
title: Text('查看一周餐食信息'),
onTap: () async => _showToast((await _dimigoinMeal.getWeeklyMeal()).toString()),
),
ListTile(
leading: Icon(Icons.fastfood_rounded),
title: Text('查看今日餐食信息'),
onTap: () async => _showToast((await _dimigoinMeal.getDailyMeal(true)).toString()),
),
ListTile(
leading: Icon(Icons.list_alt_rounded),
title: Text('查看一周课程表'),
onTap: () async => _showToast((await _dimigoinTimetable.getWeeklyTimeTable(2, 3)).toString()),
),
ListTile(
leading: Icon(Icons.fastfood_rounded),
title: Text('Dalgeurak API - 查询餐食相关信息'),
onTap: () async {
dynamic result = await _dalgeurakService.getUserMealInfo();
print(result.toString());
//_showToast((result['content'][0]['breakfast'][0] as DalgeurakConvenienceFood).student!.toJson().toString());
}
),
],
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _dimigoinAccount.refreshAccessToken(),
child: Icon(Icons.refresh_rounded),
),
);
}
void _showLoginDialog(BuildContext context) {
final userIdTextController = TextEditingController();
final passwordTextController = TextEditingController();
showDialog(
context: context,
builder: (BuildContext ctx) {
return AlertDialog(
title: Text('登录对话框'),
content: SizedBox(
height: _height * 0.3,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: _width * 0.5,
child: TextField(
keyboardType: TextInputType.name,
controller: userIdTextController,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(0, 16, 0, 0),
labelText: "ID",
),
),
),
SizedBox(
width: _width * 0.5,
child: TextField(
keyboardType: TextInputType.text,
controller: passwordTextController,
obscureText: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(0, 16, 0, 0),
labelText: "密码",
),
),
),
SizedBox(height: _height * 0.075),
GestureDetector(
onTap: () async {
Map loginResult = await _dimigoinAccount.login(userIdTextController.text, passwordTextController.text, true);
loginResult['success'] ? _showToast("登录成功") : _showToast("登录失败");
},
child: Container(
width: _width * 0.4,
height: _height * 0.05,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(16),
),
child: Center(child: Text("登录", style: TextStyle(color: Colors.white))),
),
)
],
),
),
);
}
);
}
void _showToast(String content) => Fluttertoast.showToast(
msg: content,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Color(0xE6FFFFFF),
textColor: Colors.black,
fontSize: 13.0
);
}
更多关于Flutter插件dimigoin_flutter_plugin的使用_这是用于开发与迪米高相关的应用程序的迪米高人 Flutter 插件的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复