264 lines
7.7 KiB
Dart
264 lines
7.7 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_slidable/flutter_slidable.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:model_of_the_times/requester/requester.dart';
|
|
import 'package:model_of_the_times/utils/toast.dart';
|
|
import 'package:model_of_the_times/views/common/data_required.dart';
|
|
import 'package:model_of_the_times/views/common/full_paged_struct.dart';
|
|
|
|
class Learned extends StatefulWidget {
|
|
const Learned({super.key});
|
|
|
|
@override
|
|
State<Learned> createState() => _LearnedState();
|
|
}
|
|
|
|
class _LearnedState extends State<Learned> with SingleTickerProviderStateMixin {
|
|
late final TabController _controller;
|
|
int _index = 0;
|
|
List<Widget> items = [
|
|
const Testimonials(),
|
|
const LearnHistory()
|
|
];
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller = TabController(length: 2, vsync: this);
|
|
}
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: TabBar(
|
|
controller: _controller,
|
|
indicatorSize: TabBarIndicatorSize.tab,
|
|
labelPadding: const EdgeInsetsDirectional.all(10),
|
|
tabs: const [
|
|
Text("学习感言"),
|
|
Text("学习历史"),
|
|
],
|
|
onTap: (e){
|
|
setState(() {
|
|
_index = e;
|
|
});
|
|
},
|
|
),
|
|
body: items[_index],
|
|
floatingActionButton: _index == 0 ? FloatingActionButton.extended(onPressed: (){Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) { return const AddTestimonials(); }));}, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)), label: const Text("新建感言")) : null,
|
|
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
|
|
);
|
|
}
|
|
}
|
|
|
|
class Testimonial extends StatefulWidget {
|
|
final int id;
|
|
final String title;
|
|
final String content;
|
|
final GlobalKey<DataRequiredState> parentKey;
|
|
const Testimonial({super.key, required this.id, required this.title, required this.content, required this.parentKey});
|
|
|
|
@override
|
|
State<Testimonial> createState() => _TestimonialState();
|
|
}
|
|
|
|
class _TestimonialState extends State<Testimonial> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Slidable(
|
|
endActionPane: ActionPane(
|
|
motion: const ScrollMotion(),
|
|
children: [
|
|
SlidableAction(
|
|
onPressed: (e){
|
|
var req = GlobalInformation.getInstance().requester;
|
|
req.get(resolve("/appStudy/app/deleteStatement?id=${widget.id}")).then((value) {
|
|
var data = jsonFromResponse(value);
|
|
if (data['code'] == 200){
|
|
widget.parentKey.currentState?.update();
|
|
return;
|
|
}
|
|
if (context.mounted){
|
|
toast("message", context);
|
|
}
|
|
});
|
|
},
|
|
backgroundColor: Colors.red,
|
|
label: "删除",
|
|
spacing: 1,
|
|
)
|
|
]
|
|
),
|
|
child: Card(
|
|
child: Padding(padding: const EdgeInsets.all(10), child: ListTile(
|
|
title: Text(
|
|
widget.title
|
|
),
|
|
subtitle: Text(
|
|
widget.content,
|
|
maxLines: 3,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
)),
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class Testimonials extends StatefulWidget {
|
|
const Testimonials({super.key});
|
|
|
|
@override
|
|
State<Testimonials> createState() => _TestimonialsState();
|
|
}
|
|
|
|
class _TestimonialsState extends State<Testimonials> {
|
|
final GlobalKey<DataRequiredState> _key = GlobalKey();
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return DataRequired(
|
|
key: _key,
|
|
fetchData: (global) async {
|
|
var response = await global.requester.get(resolve("/appStudy/app/statementList"));
|
|
return jsonFromResponse(response);
|
|
},
|
|
afterLoading: (data) {
|
|
return ListView(
|
|
children: data['rows'].map<Widget>((e){
|
|
var content = e['content'];
|
|
var id = e['id'];
|
|
var title = e['title'];
|
|
return Testimonial(id: id, title: title, content: content, parentKey: _key);
|
|
}).toList()
|
|
);
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
class TestimonialsEditor extends StatefulWidget {
|
|
final String defaultTitle;
|
|
final String defaultContent;
|
|
const TestimonialsEditor({super.key, this.defaultContent = "", this.defaultTitle = ""});
|
|
|
|
@override
|
|
State<TestimonialsEditor> createState() => _TestimonialsEditorState();
|
|
}
|
|
|
|
class _TestimonialsEditorState extends State<TestimonialsEditor> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
late String _title;
|
|
late String _content;
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_title = widget.defaultTitle;
|
|
_content = widget.defaultContent;
|
|
}
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Form(
|
|
key: _formKey,
|
|
child: ListView(
|
|
children: [
|
|
TextFormField(
|
|
initialValue: _title,
|
|
decoration: const InputDecoration(
|
|
labelText: "标题"
|
|
),
|
|
validator: (value){
|
|
if (value == null || value.isEmpty){
|
|
return "请输入标题";
|
|
}
|
|
return null;
|
|
},
|
|
onSaved: (value) {if (value != null){_title = value;}},
|
|
),
|
|
TextFormField(
|
|
initialValue: _content,
|
|
decoration: const InputDecoration(
|
|
labelText: "内容"
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty){
|
|
return "请输入内容";
|
|
}
|
|
return null;
|
|
},
|
|
onSaved: (value) {if (value != null){_content = value;}},
|
|
),
|
|
SizedBox.fromSize(size: const Size.fromHeight(20),),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
if (_formKey.currentState == null){
|
|
return;
|
|
}
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
_formKey.currentState!.save();
|
|
var global = GlobalInformation.getInstance();
|
|
var createUri = resolve("appStudy/app/createStatement");
|
|
var req = MultipartRequest("POST", createUri);
|
|
req.fields.addAll({"title": _title, "content": _content, "picPath": ""});
|
|
var result = await global.requester.send(req);
|
|
var data = await jsonFromStreamResponse(result);
|
|
if (context.mounted){
|
|
if (data['code'] != 200){
|
|
toast("添加失败,请重试!", context);
|
|
}
|
|
Navigator.pop(context);
|
|
}
|
|
},
|
|
child: const Text("提交")
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
class AddTestimonials extends StatefulWidget {
|
|
const AddTestimonials({super.key});
|
|
|
|
@override
|
|
State<AddTestimonials> createState() => _AddTestimonialsState();
|
|
}
|
|
|
|
class _AddTestimonialsState extends State<AddTestimonials> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const FullPagedStruct(title: "新建感言", child: TestimonialsEditor());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class LearnHistory extends StatefulWidget {
|
|
const LearnHistory({super.key});
|
|
|
|
@override
|
|
State<LearnHistory> createState() => _LearnHistoryState();
|
|
}
|
|
|
|
class _LearnHistoryState extends State<LearnHistory> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return DataRequired(
|
|
fetchData: (global) async {
|
|
var req = global.requester;
|
|
var historyResponse = await req.get(resolve("/appStudy/app/historyList"));
|
|
return jsonFromResponse(historyResponse);
|
|
},
|
|
afterLoading: (data){
|
|
if (kDebugMode) {
|
|
print(data);
|
|
}
|
|
return ListView();
|
|
}
|
|
);
|
|
}
|
|
}
|