Skip to content

Instantly share code, notes, and snippets.

@talregev
Last active May 31, 2021 19:37
Show Gist options
  • Save talregev/f98d3e1d53db850d052af5393f3b5c3c to your computer and use it in GitHub Desktop.
Save talregev/f98d3e1d53db850d052af5393f3b5c3c to your computer and use it in GitHub Desktop.
boinc dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
static _MyAppState? of(BuildContext context) =>
context.findAncestorStateOfType<_MyAppState>();
}
class _MyAppState extends State<MyApp> {
/// 1) our themeMode "state" field
ThemeMode _themeMode = ThemeMode.system;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Boinc',
theme: ThemeData(),
darkTheme: ThemeData.dark(),
themeMode: _themeMode, // 2) ← ← ← use "state" field here //////////////
home: MyHomePage(title: 'Boinc app'),
);
}
/// 3) Call this to change theme from any context using "of" accessor
/// e.g.:
/// MyApp.of(context).changeTheme(ThemeMode.dark);
void changeTheme(ThemeMode themeMode) {
setState(() {
_themeMode = themeMode;
});
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
var appBar = AppBar(title: Text(title));
var textColor = Theme.of(context).textTheme.bodyText1?.color;
var myAppContext = MyApp.of(context);
return Scaffold(
appBar: appBar,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Choose your theme:',
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
/// //////////////////////////////////////////////////////
/// Change theme & rebuild to show it using these buttons
ElevatedButton(
onPressed: () => myAppContext?.changeTheme(ThemeMode.light),
child: Text('Light')
),
ElevatedButton(
onPressed: () => myAppContext?.changeTheme(ThemeMode.dark),
child: Text('Dark'),
),
/// //////////////////////////////////////////////////////
],
),
],
),
),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
Container(
height: appBar.preferredSize.height,
child: DrawerHeader(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
shape: BoxShape.rectangle,
),
child: Text('Boinc'),
)),
ListTile(
leading: Icon(Icons.list, color: textColor),
title: Text('Tasks'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.email, color: textColor),
title: Text('Notices'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.account_tree, color: textColor),
title: Text('Projects'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.add_box, color: textColor),
title: Text('Add project'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.settings, color: textColor),
title: Text('Preferences'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.help, color: textColor),
title: Text('Help'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.bug_report, color: textColor),
title: Text('Report Issue'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.warning, color: textColor),
title: Text('Event log'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment