Skip to content

Commit

Permalink
Implement storing print layout config
Browse files Browse the repository at this point in the history
  • Loading branch information
sukso96100 committed Jul 9, 2024
1 parent 421b739 commit fffe2ef
Showing 1 changed file with 60 additions and 16 deletions.
76 changes: 60 additions & 16 deletions lib/settings/printLayout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PrintLayoutSettings extends StatefulWidget {
const PrintLayoutSettings({Key? key}) : super(key: key);
Expand All @@ -14,12 +15,43 @@ class PrintLayoutSettings extends StatefulWidget {
}

class _PrintLayoutSettingsState extends State<PrintLayoutSettings> {
int printCanvasWidthMm = -1;
int printCanvasHeightMm = -1;
int printCanvasDpi = 203;
int printCanvasWidthPx = -1;
int printCanvasHeightPx = -1;
@override
void initState() {
super.initState();
var prefs = SharedPreferences.getInstance();
prefs.then((SharedPreferences prefs) {
setState(() {
printCanvasWidthMm = prefs.getInt('printCanvasWidthMm') ?? 70;
printCanvasHeightMm = prefs.getInt('printCanvasHeightMm') ?? 70;
printCanvasDpi = prefs.getInt('printCanvasDpi') ?? 203;
printCanvasWidthPx = mmToPx(printCanvasWidthMm, printCanvasDpi);
printCanvasHeightPx = mmToPx(printCanvasHeightMm, printCanvasDpi);
});
});
print('initState is called');
}

int mmToPx(int mm, int dpi) {
return (mm * dpi) ~/ 25.4;
}

void savePrintLayout() async {
var prefs = await SharedPreferences.getInstance();
prefs.setInt('printCanvasWidthMm', printCanvasWidthMm);
prefs.setInt('printCanvasHeightMm', printCanvasHeightMm);
prefs.setInt('printCanvasDpi', printCanvasDpi);
var resultSnackBar = SnackBar(
content: Text(
"인쇄 레이아웃 저장됨: $printCanvasWidthMm mm * $printCanvasHeightMm mm ($printCanvasDpi DPI) -> $printCanvasWidthPx px * $printCanvasHeightPx px"),
);
ScaffoldMessenger.of(context).showSnackBar(resultSnackBar);
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -50,7 +82,14 @@ class _PrintLayoutSettingsState extends State<PrintLayoutSettings> {
border: OutlineInputBorder(),
labelText: '너비 (mm)',
),
onChanged: (value) => {},
onChanged: (value) {
setState(() {
printCanvasWidthMm = int.parse(value);
printCanvasWidthPx =
mmToPx(printCanvasWidthMm, printCanvasDpi);
});
savePrintLayout();
},
))),
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 300),
Expand All @@ -63,7 +102,14 @@ class _PrintLayoutSettingsState extends State<PrintLayoutSettings> {
border: OutlineInputBorder(),
labelText: '높이 (mm)',
),
onChanged: (value) => {},
onChanged: (value) {
setState(() {
printCanvasHeightMm = int.parse(value);
printCanvasHeightPx =
mmToPx(printCanvasHeightMm, printCanvasDpi);
});
savePrintLayout();
},
))),
],
),
Expand All @@ -89,7 +135,16 @@ class _PrintLayoutSettingsState extends State<PrintLayoutSettings> {
border: OutlineInputBorder(),
labelText: '해상도 (DPI)',
),
onChanged: (value) => {},
onChanged: (value) {
setState(() {
printCanvasDpi = int.parse(value);
printCanvasWidthPx =
mmToPx(printCanvasWidthMm, printCanvasDpi);
printCanvasHeightPx =
mmToPx(printCanvasHeightMm, printCanvasDpi);
});
savePrintLayout();
},
))),
],
),
Expand All @@ -98,19 +153,8 @@ class _PrintLayoutSettingsState extends State<PrintLayoutSettings> {
children: [
Container(
padding: const EdgeInsets.all(16.0),
child: OutlinedButton(
onPressed: () async {
var resultSnackBar = SnackBar(
content: Text(
"행사 관계자를 호출 하였습니다. Event staff has been called."),
);
ScaffoldMessenger.of(context)
.showSnackBar(resultSnackBar);
},
child: Text(
"설정 저장",
style: TextStyle(fontSize: 20),
))),
child: Text(
"사용중인 설정: $printCanvasWidthMm mm * $printCanvasHeightMm mm ($printCanvasDpi DPI) -> $printCanvasWidthPx px * $printCanvasHeightPx px"))
],
),
],
Expand Down

0 comments on commit fffe2ef

Please sign in to comment.