全局状态2

zhangrenshu
LRC 2 years ago
parent bd8ea17214
commit 3caa8daadf

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:timemanagerapp/setting/Setting.dart';
import 'package:timemanagerapp/widgets/HomeWidget.dart';
import 'package:timemanagerapp/provider/TimeProvider.dart';
init() async {
WidgetsFlutterBinding.ensureInitialized();
@ -18,10 +20,15 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Setting.deviceWidth = MediaQuery.of(context).size.width;
return MaterialApp(
home: Scaffold(
body: HomeWidget(),
return MultiProvider( //Timetable
providers: [
ChangeNotifierProvider(create: (ctx) => TimeProvider()),
],
child: MaterialApp(
home: Scaffold(
body: HomeWidget(),
),
),
);
); // MaterialApp
}
}

@ -0,0 +1,18 @@
import 'package:flutter/cupertino.dart';
class TimeProvider extends ChangeNotifier {
int _updatTimtTablecount = 0;
int get updatTimtTablecount => _updatTimtTablecount;
set updatTimtTablecount(int value) {
_updatTimtTablecount = value;
notifyListeners();
}
void updateTimetable() {
_updatTimtTablecount = ~_updatTimtTablecount;
notifyListeners();
}
}

@ -1,8 +1,11 @@
import 'package:flutter/material.dart';
import 'package:multi_select_flutter/dialog/multi_select_dialog_field.dart';
import 'package:multi_select_flutter/util/multi_select_item.dart';
import 'package:provider/provider.dart';
import 'package:timemanagerapp/controller/CourseController.dart';
import 'package:timemanagerapp/entity/CourseForm.dart';
import '../provider/TimeProvider.dart';
class AddCourseFormWidget extends StatefulWidget {
const AddCourseFormWidget({Key? key}) : super(key: key);
@ -181,27 +184,39 @@ class _AddCourseFormWidgetState extends State<AddCourseFormWidget> {
},
child: Text('取消'),
),
ElevatedButton(
onPressed: () {
//
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
//
courseForm.course = course;
courseForm.credit = double.parse(credit);
courseForm.note = note;
courseForm.location = location;
courseForm.selectedDays = selectedDays.map((e) => weekdayMap[e]!).toList();
courseForm.startWeek = int.parse(startWeek);
courseForm.endWeek = int.parse(endWeek);
courseForm.startTime = int.parse(startTime);
courseForm.endTime = int.parse(endTime);
courseForm.teacher = teacher;
courseController.addCourseForm(courseForm).then((value) => Navigator.pop(context)); //
Selector<TimeProvider, TimeProvider>( // time
selector: (ctx, provider) => provider,
shouldRebuild: (pre, next) => false,
builder: (ctx, timePro, child) {
return ElevatedButton(
onPressed: () {
//
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
//
courseForm.course = course;
courseForm.credit = double.parse(credit);
courseForm.note = note;
courseForm.location = location;
courseForm.selectedDays = selectedDays.map((e) => weekdayMap[e]!).toList();
courseForm.startWeek = int.parse(startWeek);
courseForm.endWeek = int.parse(endWeek);
courseForm.startTime = int.parse(startTime);
courseForm.endTime = int.parse(endTime);
courseForm.teacher = teacher;
courseController.addCourseForm(courseForm).then(((value){
//
timePro.updateTimetable();
timePro.updatTimtTablecount = 100;
Navigator.pop(context);
}
}));
};
},
child: Text('确定'),
);
},
child: Text('确定'),
// child: Icon(Icons.add),
),
],
),

@ -0,0 +1,87 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:timemanagerapp/controller/courseController.dart';
import '../entity/course.dart';
import '../provider/TimeProvider.dart';
class AutoImportWidget extends StatefulWidget {
@override
_AutoImportWidgetState createState() => _AutoImportWidgetState();
}
class _AutoImportWidgetState extends State<AutoImportWidget> {
final CourseController courseController = CourseController.getInstance();
final TextEditingController stuIdController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController yearController = TextEditingController();
final TextEditingController termController = TextEditingController();
Future<void> handleAutoImport(BuildContext context) async {
int stuId = int.parse(stuIdController.text);
String password = passwordController.text;
int year = int.parse(yearController.text);
int term = int.parse(termController.text);
int res = await courseController.autoImportCours(stuId, password,year,term);
if(res!=0) {
Provider.of<TimeProvider>(context, listen: false).updateTimetable(); //
Navigator.pop(context);
}else{
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('用户名或密码错误'))
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('自动导入校园账号课程'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: stuIdController,
decoration: InputDecoration(
labelText: '账号',
),
),
SizedBox(height: 16.0),
TextFormField(
controller: passwordController,
obscureText: true, //
decoration: InputDecoration(
labelText: '密码',
),
),
SizedBox(height: 16.0),
TextFormField(
controller: yearController,
decoration: InputDecoration(
labelText: '学年',
),
),
SizedBox(height: 16.0),
TextFormField(
controller: termController,
decoration: InputDecoration(
labelText: '学期',
),
),
SizedBox(height: 24.0),
ElevatedButton(
onPressed: () => handleAutoImport(context), // context
child: Text('导入'),
),
],
),
),
),
);
}
}

@ -2,16 +2,19 @@ import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:timemanagerapp/controller/CourseController.dart';
import 'package:timemanagerapp/controller/CourseWidgetController.dart';
import '../entity/Course.dart';
import '../provider/TimeProvider.dart';
import '../setting/Setting.dart';
class TimetableWidget extends StatefulWidget {
final double deviceWidth=Setting.deviceWidth;
final double deviceWidth = Setting.deviceWidth;
@override
State<StatefulWidget> createState() => TimetableWidgetState(deviceWidth: deviceWidth);
State<StatefulWidget> createState() =>
TimetableWidgetState(deviceWidth: deviceWidth);
}
class TimetableWidgetState extends State<TimetableWidget> {
@ -71,318 +74,351 @@ class TimetableWidgetState extends State<TimetableWidget> {
}
}
//
@override
initState() {
super.initState();
courseController.getCourses().then((res) {
courseList = res;
//
//
// courseList = await courseController.getCourses();
//
var mondayTime = courseWidgetController.getmondayTime();
currentWeek = courseWidgetController.getWeekCount();
weekCount = currentWeek;
// courseList = CourseWidgetController.testcourseList;
weekCount = courseWidgetController.getWeekCount();
courseWeekMap = courseWidgetController.transformCourseMap(courseList);
Future<void> futureDo() async {
courseList = await courseController.getCourses();
//
//
// courseList = await courseController.getCourses();
//
var mondayTime = courseWidgetController.getmondayTime();
currentWeek = courseWidgetController.getWeekCount();
weekCount = currentWeek;
// courseList = CourseWidgetController.testcourseList;
weekCount = courseWidgetController.getWeekCount();
courseWeekMap = courseWidgetController.transformCourseMap(courseList);
//
for (int i = 0; i < 7; i++) {
dateListstr.add((mondayTime.day + i).toString());
if ((mondayTime.day + i) == DateTime.now().day) {
currentWeekDayIndex = i + 1;
}
//
for (int i = 0; i < 7; i++) {
dateListstr.add((mondayTime.day + i).toString());
if ((mondayTime.day + i) == DateTime.now().day) {
currentWeekDayIndex = i + 1;
}
// print('Recent monday '+DateTime.now().day.toString());
}
// print('Recent monday '+DateTime.now().day.toString());
//
positions =
courseWidgetController.convertTimeList(timePoints, deviceWidth);
//
positions = courseWidgetController.convertTimeList(timePoints, deviceWidth);
}
setState(() {
loading = false;
});
});
//
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
if (loading == false) {
return GestureDetector(
onHorizontalDragEnd: (details) {
if (details.primaryVelocity! > 0) {
//
setState(() {
weekCount--;
updateDateByWeekCount();
});
} else if (details.primaryVelocity! < 0) {
//
setState(() {
weekCount++;
updateDateByWeekCount();
});
}
},
child: Column(
mainAxisAlignment: MainAxisAlignment.start, //
children: [
SizedBox(
//
//
child: GridView.builder(
shrinkWrap: true,
//
physics: NeverScrollableScrollPhysics(),
//
itemCount: 8,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 8, childAspectRatio: 1 / 1),
//
itemBuilder: (BuildContext context, int index) {
//item
return Container(
color: index == this.currentWeekDayIndex
? Color(0xf7f7f7) //
: Colors.white,
child: Center(
child: index == 0
? Column(
//
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
// height: 10,
// width: 6,
child: Text(
'' + weekCount.toString() + '', //
style: TextStyle(
fontSize: 12,
color: currentWeek ==
weekCount //
? Colors.amber
: Colors.black87)),
),
],
)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(weekList[index - 1], //
style: TextStyle(
fontSize: 14,
color: index ==
currentWeekDayIndex //
? Colors.lightBlue
: Colors.black87)),
SizedBox(
height: 5,
),
Text(dateListstr[index - 1], //
style: TextStyle(
fontSize: 12,
color: index ==
currentWeekDayIndex //
? Colors.lightBlue
: Colors.black87)),
],
),
),
);
}),
),
//stack
Expanded(
child: SingleChildScrollView(
child: Row(
children: [
//stack
Container(
width: deviceWidth,
height: 2000,
child: Stack(
alignment: Alignment.center,
children: [
// Stack
Positioned(
top: 0,
left: 0,
child: Container(
width: deviceWidth,
height: 2000,
child: Stack(
children: List.generate(
//
positions.length,
(index) => Positioned(
top: positions[index].dy,
left: positions[index].dx,
child: Row(
children: [
Text(
timePoints[index]
.hour
.toString()
.padLeft(2, '0') +
':' +
timePoints[index]
.minute
.toString()
.padLeft(2, '0'),
),
Container(
width: deviceWidth * 0.04,
height: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.amber,
width: 2,
),
Widget build(BuildContext contexvoidt) {
return Consumer<TimeProvider>(
builder: (ctx, timePro, child) {
print('Rebuild timePro.updatTimtTablecount = ' + timePro.updatTimtTablecount.toString());
return FutureBuilder<void>(
future: futureDo(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
//
if (snapshot.connectionState == ConnectionState.done) {
return RefreshIndicator(
onRefresh: () {
return futureDo().then((value) => setState(() {}));
},
child: GestureDetector(
onHorizontalDragEnd: (details) {
if (details.primaryVelocity! > 0) {
//
setState(() {
weekCount--;
updateDateByWeekCount();
});
} else if (details.primaryVelocity! < 0) {
//
setState(() {
weekCount++;
updateDateByWeekCount();
});
}
},
child: Column(
mainAxisAlignment: MainAxisAlignment.start, //
children: [
SizedBox(
//
//
child: GridView.builder(
shrinkWrap: true,
//
physics: NeverScrollableScrollPhysics(),
//
itemCount: 8,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 8, childAspectRatio: 1 / 1),
//
itemBuilder: (BuildContext context, int index) {
//item
return Container(
color: index == this.currentWeekDayIndex
? Color(0xf7f7f7) //
: Colors.white,
child: Center(
child: index == 0
? Column(
//
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Container(
// height: 10,
// width: 6,
child: Text(
'' +
weekCount.toString() +
'', //
style: TextStyle(
fontSize: 12,
color: currentWeek ==
weekCount //
? Colors.amber
: Colors.black87)),
),
],
)
: Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Text(weekList[index - 1], //
style: TextStyle(
fontSize: 14,
color: index ==
currentWeekDayIndex //
? Colors.lightBlue
: Colors.black87)),
SizedBox(
height: 5,
),
),
Container(
width: deviceWidth * 0.84,
height: 2,
color: const Color.fromARGB(
255, 136, 61, 61),
),
],
Text(dateListstr[index - 1], //
style: TextStyle(
fontSize: 12,
color: index ==
currentWeekDayIndex //
? Colors.lightBlue
: Colors.black87)),
],
),
),
),
),
),
)),
//Stack
Container(
constraints:
BoxConstraints.expand(), // 使constraints
// width: 390,
// height: 2000,
child: Stack(
children: List.generate(
//
courseWeekMap.containsKey(weekCount)
? courseWeekMap[weekCount]!.length
: 0,
((index) => Positioned(
top: courseWeekMap[weekCount]![index]
.getdy() +
10,
left: courseWeekMap[weekCount]![index]
.getdx() +
deviceWidth*0.15,
child: SingleChildScrollView(
child: Container(
//
width: deviceWidth * 0.115,
height:
courseWeekMap[weekCount]![index]
.getHeight(),
decoration: BoxDecoration(
color: Colors.tealAccent,
//
borderRadius: BorderRadius.all(
Radius.circular(
10.0)), //
),
child: SingleChildScrollView(
child: Column(
//
children: [
Text(
courseWeekMap[weekCount]![
index]
.name,
style: TextStyle(
fontSize: 10,
fontWeight:
FontWeight.bold),
overflow: TextOverflow
.clip, //name
),
Text(
courseWeekMap[weekCount]![
index]
.teacher,
style:
TextStyle(fontSize: 8),
overflow: TextOverflow.clip,
),
Text(
courseWeekMap[weekCount]![
index]
.location,
style:
TextStyle(fontSize: 10),
overflow: TextOverflow.clip,
),
Text(
courseWeekMap[weekCount]![
index]
.start
.hour
.toString() +
':' +
courseWeekMap[
weekCount]![
index]
.start
.minute
.toString(),
style:
TextStyle(fontSize: 10),
overflow: TextOverflow.clip,
),
Text(
courseWeekMap[weekCount]![
index]
.end
.hour
.toString() +
':' +
courseWeekMap[
weekCount]![
index]
.end
.minute
.toString(),
style:
TextStyle(fontSize: 10),
overflow: TextOverflow.clip,
),
Text(
courseWeekMap[weekCount]![
index]
.remark,
style:
TextStyle(fontSize: 10),
overflow: TextOverflow.clip,
);
}),
),
//stack
Expanded(
child: SingleChildScrollView(
child: Row(
children: [
//stack
Container(
width: deviceWidth,
height: 2000,
child: Stack(
alignment: Alignment.center,
children: [
// Stack
Positioned(
top: 0,
left: 0,
child: Container(
width: deviceWidth,
height: 2000,
child: Stack(
children: List.generate(
//
positions.length,
(index) => Positioned(
top: positions[index].dy,
left: positions[index].dx,
child: Row(
children: [
Text(
timePoints[index]
.hour
.toString()
.padLeft(2, '0') +
':' +
timePoints[index]
.minute
.toString()
.padLeft(2, '0'),
),
Container(
width: deviceWidth * 0.04,
height: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.amber,
width: 2,
),
),
),
Container(
width: deviceWidth * 0.84,
height: 2,
color: const Color.fromARGB(
255, 136, 61, 61),
),
],
),
),
),
],
),
),
),
),
))),
))
],
),
)),
//Stack
Container(
constraints: BoxConstraints
.expand(), // 使constraints
// width: 390,
// height: 2000,
child: Stack(
children: List.generate(
//
courseWeekMap.containsKey(weekCount)
? courseWeekMap[weekCount]!.length
: 0,
((index) => Positioned(
top: courseWeekMap[weekCount]![
index]
.getdy() +
10,
left: courseWeekMap[weekCount]![
index]
.getdx() +
deviceWidth * 0.15,
child: SingleChildScrollView(
child: Container(
//
width: deviceWidth * 0.115,
height: courseWeekMap[
weekCount]![index]
.getHeight(),
decoration: BoxDecoration(
color: Colors.tealAccent,
//
borderRadius: BorderRadius
.all(Radius.circular(
10.0)), //
),
child:
SingleChildScrollView(
child: Column(
//
children: [
Text(
courseWeekMap[
weekCount]![
index]
.name,
style: TextStyle(
fontSize: 10,
fontWeight:
FontWeight
.bold),
overflow: TextOverflow
.clip, //name
),
Text(
courseWeekMap[
weekCount]![
index]
.teacher,
style: TextStyle(
fontSize: 8),
overflow:
TextOverflow
.clip,
),
Text(
courseWeekMap[
weekCount]![
index]
.location,
style: TextStyle(
fontSize: 10),
overflow:
TextOverflow
.clip,
),
Text(
courseWeekMap[weekCount]![
index]
.start
.hour
.toString() +
':' +
courseWeekMap[
weekCount]![
index]
.start
.minute
.toString(),
style: TextStyle(
fontSize: 10),
overflow:
TextOverflow
.clip,
),
Text(
courseWeekMap[weekCount]![
index]
.end
.hour
.toString() +
':' +
courseWeekMap[
weekCount]![
index]
.end
.minute
.toString(),
style: TextStyle(
fontSize: 10),
overflow:
TextOverflow
.clip,
),
Text(
courseWeekMap[
weekCount]![
index]
.remark,
style: TextStyle(
fontSize: 10),
overflow:
TextOverflow
.clip,
),
],
),
),
),
),
))),
))
],
),
),
],
),
))
],
),
),
),
],
),
))
],
),
);
}
return Container(
child: Text('加载中'),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
});
}
);
}
}

@ -168,6 +168,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.1.3"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
path:
dependency: "direct main"
description:
@ -240,6 +248,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.6"
provider:
dependency: "direct main"
description:
name: provider
sha256: "59471e0a4595e264625d3496af567ac85bdae1148ec985aff1e0555786f53ecf"
url: "https://pub.flutter-io.cn"
source: hosted
version: "5.0.0"
shared_preferences:
dependency: "direct main"
description:

@ -42,6 +42,7 @@ dependencies:
multi_select_flutter: 4.1.3
http: ^1.1.0
device_info:
provider: ^5.0.0
dev_dependencies:

Loading…
Cancel
Save