1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 06:26:02 +00:00
AideDeJeu/aidedejeu_flutter/lib/widgets/library.dart

297 lines
7.9 KiB
Dart
Raw Normal View History

2020-03-22 20:36:08 +01:00
import 'package:aidedejeu_flutter/databases/database.dart';
2020-03-24 14:01:42 +01:00
import 'package:aidedejeu_flutter/databases/database_sqflite.dart';
2020-03-02 17:46:30 +01:00
import 'package:aidedejeu_flutter/localization.dart';
2020-02-29 01:40:05 +01:00
import 'package:aidedejeu_flutter/models/filters.dart';
import 'package:aidedejeu_flutter/widgets/filters.dart';
import 'package:aidedejeu_flutter/models/items.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_svg/flutter_svg.dart';
2020-03-01 19:04:48 +01:00
import '../theme.dart';
2020-02-29 01:40:05 +01:00
class LibraryPage extends StatefulWidget {
LibraryPage({Key key, @required this.id}) : super(key: key);
final String id;
2020-03-22 20:36:08 +01:00
final BaseDB _db = SqfliteDB.instance;
2020-02-29 01:40:05 +01:00
@override
_LibraryPageState createState() => _LibraryPageState();
}
class _LibraryPageState extends State<LibraryPage> {
2020-03-22 20:36:08 +01:00
final BaseDB _db = SqfliteDB.instance;
2020-02-29 01:40:05 +01:00
void setItem(Item item) {
setState(() {
this.item = item;
if (item is FilteredItems) {
2020-03-12 17:05:53 +01:00
this.filters = item.toFilterList();
2020-02-29 01:40:05 +01:00
} else {
this.filters = null;
}
this.markdown =
item.markdown.replaceAllMapped(RegExp(r'<!--.*?-->'), (match) {
2020-03-02 17:46:30 +01:00
return '';
});
2020-02-29 01:40:05 +01:00
});
}
String markdown = "";
2020-03-12 17:05:53 +01:00
Item item;
2020-02-29 01:40:05 +01:00
MarkdownStyleSheet styleSheet;
2020-03-12 17:05:53 +01:00
List<Filter> filters;
2020-02-29 01:40:05 +01:00
@override
void initState() {
super.initState();
_loadItem().then((item) => setItem(item));
}
@protected
@mustCallSuper
void didChangeDependencies() {
2020-03-01 19:04:48 +01:00
super.didChangeDependencies();
styleSheet = mainMarkdownStyleSheet(context);
2020-02-29 01:40:05 +01:00
}
Future<Item> _loadItem() async {
2020-03-24 14:01:42 +01:00
var item = await _db.getItemWithId(this.widget.id);
2020-03-22 20:36:08 +01:00
await _db.loadChildrenItems(item, filters);
2020-02-29 01:40:05 +01:00
return item;
}
Widget _buildMarkdown(BuildContext context) {
return Markdown(
data: markdown,
styleSheet: styleSheet,
onTapLink: (link) => Navigator.push(
context,
MaterialPageRoute(builder: (context) => LibraryPage(id: link)),
),
);
}
Widget _buildMarkdownBody(BuildContext context) {
return Container(
margin: const EdgeInsets.all(10.0),
child: MarkdownBody(
data: markdown,
styleSheet: styleSheet,
onTapLink: (link) => Navigator.push(
context,
MaterialPageRoute(builder: (context) => LibraryPage(id: link)),
),
));
}
Widget _buildChildTile(BuildContext context, Item item) {
return ListTile(
2020-03-05 16:50:31 +01:00
title: Text(
item.name,
style: TextStyle(
fontSize: 25.0,
color: colorHDBlue,
fontWeight: FontWeight.bold,
fontFamily: 'Cinzel-Regular',
),
),
2020-10-09 09:38:28 +02:00
subtitle: Text(item.altNameText ?? "",
2020-03-05 16:50:31 +01:00
style: TextStyle(
fontSize: 18.0,
color: colorHDLightGrey,
fontWeight: FontWeight.bold,
fontFamily: 'Cinzel-Regular',
),),
2020-02-29 01:40:05 +01:00
onTap: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => LibraryPage(id: item.id)),
),
);
}
Widget _buildLibraryItemPage() {
return Stack(
children: <Widget>[
ListView.builder(
itemCount: (item?.children?.length ?? 0) + 1,
itemBuilder: (BuildContext context, int index) {
return index == 0
? _buildMarkdownBody(context)
: _buildChildTile(context, item.children[index - 1]);
})
],
);
}
Widget _buildBookmarksPage() {
return Text("Bookmarks");
}
Widget _buildSearchPage() {
return Text("Search");
}
BottomNavigationBarItem _buildBottomNavigationBarItem(
String title, String assetName) {
return BottomNavigationBarItem(
icon: SvgPicture.asset(
assetName,
height: 30.0,
width: 30.0,
allowDrawingOutsideViewBox: true,
),
2020-03-05 16:50:31 +01:00
title: Text(title, style: TextStyle(fontSize: 16.0,
fontWeight: FontWeight.bold,
fontFamily: 'Cinzel-Regular',),),
2020-02-29 01:40:05 +01:00
activeIcon: SvgPicture.asset(
assetName,
height: 40.0,
width: 40.0,
allowDrawingOutsideViewBox: true,
),
);
}
List _buildBottomNavigationBarItems() {
return <BottomNavigationBarItem>[
2020-03-02 17:46:30 +01:00
_buildBottomNavigationBarItem(
2020-03-03 16:03:31 +01:00
AppLocalizations.of(context).libraryTitle,
2020-03-02 17:46:30 +01:00
"assets/spell-book.svg",
),
_buildBottomNavigationBarItem(
2020-03-03 16:03:31 +01:00
AppLocalizations.of(context).bookmarksTitle,
2020-03-02 17:46:30 +01:00
"assets/stars-stack.svg",
),
_buildBottomNavigationBarItem(
2020-03-03 16:03:31 +01:00
AppLocalizations.of(context).searchTitle,
2020-03-02 17:46:30 +01:00
"assets/crystal-ball.svg",
),
2020-02-29 01:40:05 +01:00
];
}
Widget _buildChoiceFilter(Filter filter) {
return ChipListFilter(
choices: filter.values,
selectedChoices: filter.selectedValues,
updateSelectedChoices: (Set<String> choices) {
setState(() {
filter.selectedValues = choices;
});
2020-03-22 20:36:08 +01:00
_db.loadChildrenItems(item, filters).then((value) => {
2020-03-02 17:46:30 +01:00
setState(() {
this.item = item;
this.filters = filters;
})
});
2020-02-29 01:40:05 +01:00
},
);
}
Widget _buildRangeFilter(Filter filter) {
return RangeFilter(
values: filter.values,
rangeValues: filter.rangeValues,
updateRangeValues: (RangeValues values) {
setState(() {
filter.rangeValues = values;
});
2020-03-22 20:36:08 +01:00
_db.loadChildrenItems(item, filters).then((value) => {
2020-03-02 17:46:30 +01:00
setState(() {
this.item = item;
this.filters = filters;
})
});
2020-02-29 01:40:05 +01:00
});
}
Widget _buildFilter(Filter filter) {
return Column(children: <Widget>[
Divider(
color: Colors.blueGrey,
2020-03-05 16:50:31 +01:00
height: 0.0,
2020-02-29 01:40:05 +01:00
),
Align(
alignment: Alignment.centerLeft,
child: Padding(
2020-03-05 16:50:31 +01:00
padding: const EdgeInsets.fromLTRB(8.0,1.0,8.0,1.0),
2020-03-13 09:46:17 +01:00
child: Text(AppLocalizations.of(context).translate(filter.name), style: TextStyle(fontFamily: "Cinzel"),),
2020-02-29 01:40:05 +01:00
),
),
Container(
child: filter.type == FilterType.Choices
? _buildChoiceFilter(filter)
: _buildRangeFilter(filter))
]);
}
List<Widget> _buildFilterList() {
return filters.map((filter) => _buildFilter(filter)).toList();
}
int indexPage = 0;
@override
Widget build(BuildContext context) {
print("build");
Widget currentPage;
switch (indexPage) {
case 0:
currentPage = _buildLibraryItemPage();
break;
case 1:
currentPage = _buildBookmarksPage();
break;
case 2:
currentPage = _buildSearchPage();
break;
}
return Scaffold(
//appBar: AppBar(
// title: Text(widget.id),
//),
body: currentPage,
bottomNavigationBar: BottomNavigationBar(
currentIndex: indexPage,
onTap: (int index) {
setState(() {
this.indexPage = index;
});
},
2020-03-05 16:50:31 +01:00
selectedItemColor: colorHDRed,
2020-02-29 01:40:05 +01:00
items: _buildBottomNavigationBarItems(),
),
endDrawer: filters != null
? Drawer(
2020-03-02 17:46:30 +01:00
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: _buildFilterList()),
)
2020-02-29 01:40:05 +01:00
: null,
appBar: AppBar(
2020-03-05 16:50:31 +01:00
title: Text(item?.name ?? widget.id),
2020-02-29 01:40:05 +01:00
actions: filters != null
? [
2020-03-02 17:46:30 +01:00
Builder(
builder: (context) => IconButton(
icon: SvgPicture.asset(
"assets/funnel.svg",
height: 30.0,
width: 30.0,
allowDrawingOutsideViewBox: true,
2020-03-13 09:46:17 +01:00
),
2020-03-02 17:46:30 +01:00
onPressed: () => Scaffold.of(context).openEndDrawer(),
tooltip:
MaterialLocalizations.of(context).openAppDrawerTooltip,
),
),
]
2020-02-29 01:40:05 +01:00
: null,
),
);
}
}