Skip to content

Commit

Permalink
support multi gifs (#3)
Browse files Browse the repository at this point in the history
* support multi gifs

Signed-off-by: kuromesi <[email protected]>

* handle errors, optimize logic

Signed-off-by: kuromesi <[email protected]>

---------

Signed-off-by: kuromesi <[email protected]>
  • Loading branch information
Kuromesi authored Nov 8, 2024
1 parent 7fd1f95 commit 3beb448
Show file tree
Hide file tree
Showing 13 changed files with 539 additions and 191 deletions.
156 changes: 156 additions & 0 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import 'package:flutter/material.dart';
import 'package:landscape/players/gif.dart';
import 'package:landscape/players/scroll_text.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'package:landscape/players/error.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
return MaterialApp(
title: 'Landscape',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: const Landscape(),
);
}
}

class Landscape extends StatefulWidget {
const Landscape({super.key});
@override
_LandscapeState createState() => _LandscapeState();
}

List<Widget> _pages = [ErrorPage(), GifPage(), ScrollTextPage()];

Map<String, int> _pagesMap = {
'GIF': 1,
'Scroll Text': 2,
};

class _LandscapeState extends State<Landscape> {
bool _isDarkTheme = false;
bool _keepScreenOn = false;
int _pageIndex = 1;
PageController _pageController = PageController(initialPage: 1);
final SharedPreferencesAsync _prefs = SharedPreferencesAsync();

@override
void initState() {
_loadPreferences();
super.initState();
}

@override
void dispose() {
_savePreferences();
_pageController.dispose();
super.dispose();
}

Future<void> _loadPreferences() async {
_isDarkTheme = await _prefs.getBool('isDarkTheme') ?? false;
_keepScreenOn = await _prefs.getBool('keepScreenOn') ?? false;
setState(() {
_isDarkTheme = _isDarkTheme;
_keepScreenOn = _keepScreenOn;
});
}

Future<void> _savePreferences() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool("isDarkTheme", _isDarkTheme);
await prefs.setBool("keepScreenOn", _keepScreenOn);
}

void _toggleTheme() {
setState(() {
_isDarkTheme = !_isDarkTheme;
});
_prefs.setBool("isDarkTheme", _isDarkTheme);
}

void _showPage(String page) {
setState(() {
_pageIndex = _pagesMap[page] ?? 0;
_pageController.jumpToPage(_pageIndex);
});
}

void _toggleKeepScreenOn(BuildContext context) {
_keepScreenOn = !_keepScreenOn;
WakelockPlus.toggle(enable: _keepScreenOn);
setState(() {
_keepScreenOn = _keepScreenOn;
});
final snackBar = SnackBar(
content: Text(_keepScreenOn ? 'Wakelock Enabled' : 'Wakelock Disabled'),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: _isDarkTheme ? Colors.grey : Colors.purple,
brightness: _isDarkTheme ? Brightness.dark : Brightness.light,
scaffoldBackgroundColor: _isDarkTheme ? Colors.black : Colors.white,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Landscape'),
actions: [
IconButton(
icon:
Icon(_isDarkTheme ? Icons.wb_sunny : Icons.nightlight_round),
onPressed: _toggleTheme,
),
Builder(
builder: (context) => IconButton(
icon: Icon(_keepScreenOn ? Icons.lock : Icons.lock_open),
onPressed: () => _toggleKeepScreenOn(context),
),
)
],
),
body: PageView(
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
children: _pages,
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: _isDarkTheme ? Colors.grey[800] : Colors.purple,
),
child: const Text('Players'),
),
ListTile(
title: const Text('GIF'),
onTap: () {
_showPage("GIF");
},
),
ListTile(
title: const Text('Scroll Text'),
onTap: () {
_showPage("Scroll Text");
},
),
],
),
),
),
);
}
}
10 changes: 10 additions & 0 deletions lib/logs/notify.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:flutter/material.dart';


void notify(BuildContext context, String text) {
final snackBar = SnackBar(
content: Text(text),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
156 changes: 2 additions & 154 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,158 +1,6 @@
import 'package:flutter/material.dart';
import 'package:landscape/players/gif.dart';
import 'package:landscape/players/scroll_text.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'package:landscape/players/error.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:landscape/app.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Landscape',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: const Landscape(),
);
}
}

class Landscape extends StatefulWidget {
const Landscape({super.key});
@override
_LandscapeState createState() => _LandscapeState();
}

List<Widget> _pages = [ErrorPage(), GifPage(), ScrollTextPage()];

Map<String, int> _pagesMap = {
'GIF': 1,
'Scroll Text': 2,
};

class _LandscapeState extends State<Landscape> {
bool _isDarkTheme = false;
bool _keepScreenOn = false;
int _pageIndex = 1;
PageController _pageController = PageController(initialPage: 1);
final SharedPreferencesAsync _prefs = SharedPreferencesAsync();

@override
void initState() {
_loadPreferences();
super.initState();
}

@override
void dispose() {
_savePreferences();
_pageController.dispose();
super.dispose();
}

Future<void> _loadPreferences() async {
_isDarkTheme = await _prefs.getBool('isDarkTheme') ?? false;
_keepScreenOn = await _prefs.getBool('keepScreenOn') ?? false;
setState(() {
_isDarkTheme = _isDarkTheme;
_keepScreenOn = _keepScreenOn;
});
}

Future<void> _savePreferences() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool("isDarkTheme", _isDarkTheme);
await prefs.setBool("keepScreenOn", _keepScreenOn);
}

void _toggleTheme() {
setState(() {
_isDarkTheme = !_isDarkTheme;
});
_prefs.setBool("isDarkTheme", _isDarkTheme);
}

void _showPage(String page) {
setState(() {
_pageIndex = _pagesMap[page] ?? 0;
_pageController.jumpToPage(_pageIndex);
});
}

void _toggleKeepScreenOn(BuildContext context) {
_keepScreenOn = !_keepScreenOn;
WakelockPlus.toggle(enable: _keepScreenOn);
setState(() {
_keepScreenOn = _keepScreenOn;
});
final snackBar = SnackBar(
content: Text(_keepScreenOn ? 'Wakelock Enabled' : 'Wakelock Disabled'),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: _isDarkTheme ? Colors.grey : Colors.purple,
brightness: _isDarkTheme ? Brightness.dark : Brightness.light,
scaffoldBackgroundColor: _isDarkTheme ? Colors.black : Colors.white,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Landscape'),
actions: [
IconButton(
icon:
Icon(_isDarkTheme ? Icons.wb_sunny : Icons.nightlight_round),
onPressed: _toggleTheme,
),
Builder(
builder: (context) => IconButton(
icon: Icon(_keepScreenOn ? Icons.lock : Icons.lock_open),
onPressed: () => _toggleKeepScreenOn(context),
),
)
],
),
body: PageView(
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
children: _pages,
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: _isDarkTheme ? Colors.grey[800] : Colors.purple,
),
child: const Text('Players'),
),
ListTile(
title: const Text('GIF'),
onTap: () {
_showPage("GIF");
},
),
ListTile(
title: const Text('Scroll Text'),
onTap: () {
_showPage("Scroll Text");
},
),
],
),
),
),
);
}
}
}
Loading

0 comments on commit 3beb448

Please sign in to comment.