1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 22:45:44 +00:00
This commit is contained in:
Yan Maniez 2020-02-14 10:37:58 +01:00
parent 57c792a1cf
commit fa0e36faa8
12 changed files with 212 additions and 79 deletions

View file

@ -1,111 +1,173 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
void main() => runApp(MyApp());
class Item {
String Id;
String Markdown;
Item({this.Id, this.Markdown});
factory Item.fromMap(Map<String, dynamic> json) => new Item(
Id: json["Id"],
Markdown: json["Markdown"],
);
Map<String, dynamic> toMap() => {
"Id": Id,
"Markdown": Markdown,
};
}
Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await getDatabaseInstance();
return _database;
}
Future<Database> getDatabaseInstance() async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "library.db");
var exists = await databaseExists(path);
if (!exists) {
print("Creating new copy from asset");
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
ByteData data = await rootBundle.load(join("assets", "library.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes, flush: true);
} else {
print("Opening existing database");
}
return await openDatabase(path, readOnly: true);
}
Future<Item> getItemWithId(String id) async {
print("getItemWithId " + id);
final db = await database;
var response = await db
.query("Items", where: "Id = ? OR RootId = ?", whereArgs: [id, id]);
if (response.isEmpty) {
print("Id not found");
}
return response.isNotEmpty ? Item.fromMap(response.first) : null;
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
primarySwatch: Colors.red,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
home: MyHomePage(id: 'index.md'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({Key key, @required this.id}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
final String id;
@override
_MyHomePageState createState() => _MyHomePageState();
_MyHomePageState createState() => _MyHomePageState(id: this.id);
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
_MyHomePageState({@required this.id});
void _incrementCounter() {
final String id;
void setMarkdown(String md) {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
markdown = md.replaceAllMapped(RegExp(r'<!--.*?-->'), (match) {
return '';
});
});
}
String markdown = "";
MarkdownStyleSheet styleSheet;
@override
void initState() {
super.initState();
ThemeData theme = ThemeData(
brightness: Brightness.light,
primaryColor: Colors.lightBlue[800],
accentColor: Colors.cyan[600],
fontFamily: 'LinuxLibertine',
textTheme: TextTheme(
headline: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
title: TextStyle(fontSize: 18.0, fontStyle: FontStyle.italic),
body1: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
),
);
//styleSheet = MarkdownStyleSheet.fromTheme(theme);
styleSheet = MarkdownStyleSheet(
tableColumnWidth: IntrinsicColumnWidth(),
tableCellsPadding: EdgeInsets.all(0.2));
getItemWithId(this.id)
.then((item) => setMarkdown(item.Markdown))
.catchError((error) => print(error));
}
final Widget svg = SvgPicture.asset("assets/crystal-ball.svg",
height: 20.0,
width: 20.0,
allowDrawingOutsideViewBox: true,
);
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
title: Text(widget.id),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
body: Markdown(
data: markdown,
styleSheet: styleSheet,
onTapLink: (link) => Navigator.push(context,
MaterialPageRoute(builder: (context) => MyHomePage(id: link)))),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
//icon: svg,
title: Text('School'),
//activeIcon: Icon(Icons.category, color: Color(0xFFEF5123)),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}