Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Last active March 21, 2023 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-beebe/dba4e04bb12b1d6255638870d122738c to your computer and use it in GitHub Desktop.
Save jonathan-beebe/dba4e04bb12b1d6255638870d122738c to your computer and use it in GitHub Desktop.
DartPad Playground
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'dart:math';
void main() => runApp(
ChangeNotifierProvider(
create: (_) => ThemeProvider(),
child: const MyApp(),
),
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const String _title = 'Flutter Code Sample';
static final ThemeData _themeLight = ThemeData(
brightness: Brightness.light,
// primaryColor: Colors.blueGrey,
useMaterial3: true,
);
static final ThemeData _themeDark = ThemeData(
brightness: Brightness.dark,
// primaryColor: Colors.blueGrey,
useMaterial3: true,
);
@override
Widget build(BuildContext context) {
return Consumer<ThemeProvider>(
builder: (context, themeProvider, _) {
return MaterialApp(
theme: _themeLight,
darkTheme: _themeDark,
themeMode: themeProvider.mode,
title: _title,
home: const MyStatefulWidget(),
);
},
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Center(child: Text('Sample Code')),
actions: <Widget>[
TextButton(
onPressed: () {},
child: const Text('Action 1'),
),
TextButton(
onPressed: () {},
child: const Text('Action 2'),
),
IconButton(
icon: const Icon(Icons.toggle_off),
onPressed: () {
Provider.of<ThemeProvider>(context, listen: false).toggleMode();
},
),
],
),
drawer: Drawer(
child: ListView(
children: <Widget>[
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Drawer Header',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: const Icon(Icons.message),
title: Text('$_count Messages'),
),
const ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
const ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
),
body: Container(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: 9,
itemBuilder: (context, index) {
return Container(
padding: const EdgeInsets.all(16),
width: double.maxFinite,
child: CustomCard());
},
))
],
),
),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: Container(height: 50.0),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
_count++;
}),
tooltip: 'Increment Counter',
child: const Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
}
int getRandomIntInRange(int min, int max) {
final random = Random();
return min + random.nextInt(max - min + 1);
}
String generateRandomParagraphs(int count) {
const paragraphs = [
'Fusce volutpat metus vitae tristique pretium. Etiam ac velit ultricies, consectetur augue vel, dapibus ligula. Morbi dignissim lorem a augue hendrerit, nec commodo purus eleifend. In scelerisque quam at fermentum porttitor. Aliquam lobortis condimentum risus, id feugiat velit sollicitudin eu. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
'Suspendisse congue magna at auctor sollicitudin. Vivamus lacinia lorem justo, vitae vestibulum odio bibendum ac. Proin sit amet mauris nec ante sodales pulvinar in vitae velit. Aenean tempor ex nulla, ut placerat turpis fermentum non. Nunc ullamcorper purus ut tincidunt pharetra. Integer vitae vehicula libero, vel efficitur lorem.',
'A quaint hamlet nestled in the verdant valley, where the mellifluous song of a babbling brook accompanied the harmonious hum of daily life. The denizens, bound by an unspoken camaraderie, went about their tasks with diligence and grace. The aroma of freshly baked bread wafted through the air, intermingling with the fragrant scent of blossoming flowers. As twilight approached, the villagers gathered in the town square to share stories and laughter, weaving a tapestry of shared memories and dreams.',
'In the sunlit realm of splendorous gardens, a wandering flock of starlings chattered with the melodious symphony of crinkling leaves. The labyrinthine pathways meandered through the vibrant tapestry of flora, as a gentle zephyr whispered secrets to the blossoms.',
'The labyrinthine pathways meandered through the vibrant tapestry of flora, as a gentle zephyr whispered secrets to the blossoms. Mysteriously, an ephemeral light flickered in the distance, evoking curiosity in the hearts of passersby. Amidst this captivating scene, the amaranthine sky painted a serene portrait, inviting contemplation and wonder.',
];
final random = Random();
final result = List<String>.generate(
count, (index) => paragraphs[random.nextInt(paragraphs.length)]);
return result.join('\n\n');
}
class CustomCard extends StatelessWidget {
final String title = 'Card Title';
final String bodyText =
'''In the sunlit realm of splendorous gardens, a wandering flock of starlings chattered with the melodious symphony of crinkling leaves. The labyrinthine pathways meandered through the vibrant tapestry of flora, as a gentle zephyr whispered secrets to the blossoms.
A quaint hamlet nestled in the verdant valley, where the mellifluous song of a babbling brook accompanied the harmonious hum of daily life. The denizens, bound by an unspoken camaraderie, went about their tasks with diligence and grace. The aroma of freshly baked bread wafted through the air, intermingling with the fragrant scent of blossoming flowers. As twilight approached, the villagers gathered in the town square to share stories and laughter, weaving a tapestry of shared memories and dreams.''';
final List<String> chips = ['Chip 1', 'Chip 2'];
@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: Theme.of(context).textTheme.headlineSmall),
SizedBox(height: 10),
Text(
style: Theme.of(context).textTheme.bodyLarge,
generateRandomParagraphs(getRandomIntInRange(1, 3)),
// overflow: TextOverflow.ellipsis,
// maxLines: getRandomIntInRange(3, 12),
),
SizedBox(height: 10),
Wrap(
spacing: 8,
children: [
ElevatedButton(
onPressed: () {
// Add your onPressed functionality here
},
child: Text('3 Anecdotes'),
),
ElevatedButton(
onPressed: () {
// Add your onPressed functionality here
},
child: Text('2 Data Points'),
),
],
// chips.map((chipText) => Chip(label: Text(chipText))).toList(),
),
],
),
),
);
}
}
// Theme Provider
class ThemeProvider with ChangeNotifier {
ThemeMode _mode = ThemeMode.light;
ThemeMode get mode => _mode;
void toggleMode() {
if (_mode == ThemeMode.light) {
_mode = ThemeMode.dark;
} else {
_mode = ThemeMode.light;
}
notifyListeners();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment