Created
August 9, 2021 18:56
-
-
Save mobibob/66a2984052df39d5bf9ac8ff05ebe570 to your computer and use it in GitHub Desktop.
From a tutorial, an accumulation of widgets from buttons, date-picker, sliders, alert dialog, etc. Some stuff is commented out to reduce the clutter for new widgets. Have fun as sample Dart / Flutter code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(new MaterialApp( | |
home: new MyApp())); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_State createState() => new _State(); | |
} | |
class _State extends State<MyApp> { | |
final GlobalKey<ScaffoldState> _scaffoldState = new GlobalKey<ScaffoldState>(); | |
Future? _showAlertDialog(BuildContext context, String alertMessage) async { | |
return showDialog( | |
context: context, | |
builder: (BuildContext context) { return new AlertDialog( | |
title: new Text("title"), | |
content: new Text("message"), | |
actions: [new TextButton( | |
onPressed: () => Navigator.pop(context), | |
child: Text("${alertMessage}") | |
)], | |
);} | |
); | |
} | |
late List<BottomNavigationBarItem> _items; | |
String _navValue = ""; | |
int _index = 0; | |
@override | |
void initState() { | |
super.initState(); | |
_items = new List.empty(growable: true); | |
_items.add(new BottomNavigationBarItem(icon: new Icon(Icons.forward), label: "Forward")); | |
_items.add(new BottomNavigationBarItem(icon: new Icon(Icons.backup), label: "Backup")); | |
_items.add(new BottomNavigationBarItem(icon: new Icon(Icons.archive), label: "Archive")); | |
} | |
String _value = "Hello"; | |
int _counter = 0; | |
String _entry = ""; | |
bool _toggle1 = false; | |
bool _toggle2 = false; | |
void _onToggle1(bool? toggle) => setState(() => _toggle1 = toggle!); | |
void _onToggle2(bool? toggle) => setState(() => _toggle2 = toggle!); | |
int _radio1 = 0; | |
int _radio2 = 0; | |
void _onRadio1(int? radio) => setState(() => _radio1 = radio!); | |
void _onRadio2(int? radio) => setState(() => _radio2 = radio!); | |
Widget makeRadios() { | |
// List list = new List<Widget>.empty(growable: true); | |
List<Widget> list = new List.empty(growable: true); | |
List titles = ["Casey", "Dawg", "Mobidawg"]; | |
for (int iRadio=0; iRadio < 3; iRadio++) { | |
list.add(Radio( | |
key: Key("Radio$iRadio"), | |
onChanged: _onRadio1, | |
value: iRadio, | |
groupValue: _radio1, | |
)); | |
} | |
Column column = new Column(children: list); | |
return column; | |
} | |
Widget makeRadioTiles() { | |
// List list = new List<Widget>.empty(growable: true); | |
List<Widget> list = new List.empty(growable: true); | |
List titles = ["Casey", "Dawg", "Mobidawg"]; | |
for (int iRadio=0; iRadio < 3; iRadio++) { | |
list.add(RadioListTile( | |
key: Key("Radio$iRadio"), | |
title: Text("${titles[iRadio]}"), | |
subtitle: Text("subtitle"), | |
onChanged: _onRadio1, | |
value: iRadio, | |
groupValue: _radio1, | |
)); | |
} | |
Column column = new Column(children: list); | |
return column; | |
} | |
double _sliderValue = 0.50; | |
void _onSliderChange(double sliderValue) => setState(() => _sliderValue = sliderValue); | |
void _onClick(String value) { | |
setState(() { | |
// _value = value; | |
if( _value == "Hello") { | |
_value = "Good-bye"; | |
} else { | |
_value = "Hello"; | |
} | |
}); | |
} | |
void _onChange(String entryText) { | |
setState(() => _entry = "Change: $entryText" ); | |
} | |
void _onSubmit(String entryText) { | |
setState(() => _entry = "Submit: $entryText" ); | |
} | |
void _add() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
void _subtract() { | |
setState(() { | |
_counter--; | |
}); | |
} | |
/// Date Picker | |
/// | |
String _date = ""; | |
Future _pickDate() async { | |
DateTime? picked = await showDatePicker( | |
context: context, | |
initialDate: new DateTime.now(), | |
firstDate: new DateTime(2016), | |
lastDate: new DateTime(2022) | |
); | |
if (picked != null) setState(() => _date = picked.toString()); | |
} | |
String _actionValue = ""; | |
void _onActionButton() => setState(() => (_actionValue = new DateTime.now().toString())); | |
int _footerButtonValue = 0; | |
void _onFooterButton(footerValue) => setState(() => ( _footerButtonValue = footerValue!)); | |
void _showBottomActionSheet() { | |
showModalBottomSheet<void>( | |
context: context, | |
builder: (BuildContext context) { | |
return new Container( | |
height: MediaQuery.of(context).size.height/4.0, | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
// new Text("Casey Dog", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),), | |
new TextButton(onPressed: () => Navigator.pop(context), child: new Text("Casey Dog")), | |
new TextButton(onPressed: () => Navigator.pop(context), child: new Text("Yukon Gold")), | |
new TextButton(onPressed: () => Navigator.pop(context), child: new Text("Green Acres")) | |
], | |
) | |
); | |
} | |
); | |
} | |
void _showSnackBar() { | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar( | |
content: const Text('snack'), | |
duration: const Duration(seconds: 1), | |
action: SnackBarAction( | |
label: 'ACTION', | |
onPressed: () { },), | |
) | |
); | |
} | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
key: _scaffoldState, | |
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("Multi-Widget Study App"), | |
backgroundColor: Colors.deepOrange, | |
), | |
drawer: new Drawer( | |
child: new Container( | |
padding: EdgeInsets.only(top: 82.0, left: MediaQuery.of(context).size.width/16), | |
height: 4.0, | |
child: Column( | |
children: <Widget>[ | |
new Text("Open"), | |
new ElevatedButton(onPressed: () { | |
setState(() => (_value = "Pop")); | |
Navigator.pop(context); | |
}, child: new Text("Close")) | |
] | |
) | |
) | |
), | |
bottomNavigationBar: new BottomNavigationBar( | |
items: _items, | |
fixedColor: Colors.blue, | |
currentIndex: _index, | |
onTap: (int index) => setState(() {_index = index;}), | |
), | |
persistentFooterButtons: [ | |
new IconButton(icon: new Icon(Icons.style), onPressed: () => _onFooterButton, color: Colors.orangeAccent,), | |
new IconButton(icon: new Icon(Icons.accessibility), onPressed: () => _onFooterButton, color: Colors.blueAccent,), | |
new IconButton(icon: new Icon(Icons.ac_unit), onPressed: () => _onFooterButton, color: Colors.purpleAccent,), | |
], | |
body: new Container( | |
color: Colors.white12, | |
padding: EdgeInsets.all(6.0), | |
child: new Column( | |
children: <Widget>[ | |
Text(_value), | |
ElevatedButton( | |
key: Key("Boggle"), | |
onPressed: () => _onClick(_value), | |
child: Text("Toggle"), | |
), | |
// Text(_counter.toString()), | |
// IconButton( | |
// key: Key("Icon1"), | |
// onPressed: () => _add(), | |
// icon: new Icon(Icons.add), | |
// ), | |
// IconButton( | |
// key: Key("Icon2"), | |
// onPressed: () => _subtract(), | |
// icon: new Icon(Icons.remove), | |
// ), | |
// Text(_entry), | |
// TextField( | |
// key: Key("Entry"), | |
// autofocus: true, | |
// keyboardType: TextInputType.emailAddress, | |
// decoration: new InputDecoration( | |
// labelText: '', | |
// hintText: 'Enter your email abddress ...', | |
// icon: new Icon(Icons.email), | |
// ), | |
// onSubmitted: _onSubmit, | |
// onChanged: _onChange, | |
// ), | |
// // --- check-boxes without any title, etc. | |
// // Checkbox(key: Key("Checkbox1"), value: _toggle1, onChanged: _onToggle1), | |
// // Checkbox(key: Key("Checkbox2"), value: _toggle2, onChanged: _onToggle2) | |
// CheckboxListTile( | |
// value: _toggle1, | |
// onChanged: _onToggle1, | |
// title: Text("Toggle this box."), | |
// controlAffinity: ListTileControlAffinity.leading, | |
// ), | |
// CheckboxListTile( | |
// value: _toggle2, | |
// onChanged: _onToggle2, | |
// title: Text("Toggle this box."), | |
// controlAffinity: ListTileControlAffinity.leading, | |
// subtitle: Text("subtitle"), | |
// secondary: new Icon(Icons.access_time), | |
// activeColor: Colors.redAccent, | |
// ) | |
makeRadioTiles(), | |
new Text("Percent ${(_sliderValue*100.0).round()}%"), | |
new Slider(key: Key("slider1"), value: _sliderValue, onChanged: _onSliderChange,), | |
new Center( | |
child: new Column( | |
children: <Widget>[ | |
Text("$_date"), | |
ElevatedButton(onPressed: _pickDate, child: new Text("Pick Date")) | |
] | |
), | |
), | |
// Text("$_actionValue"), | |
// ElevatedButton(onPressed: (true) ? _showBottomActionSheet : _showSnackBar, child: new Text((true) ? "Action Sheet" : "Snack bar")) | |
Text("$_actionValue"), | |
ElevatedButton( | |
onPressed: () => _showAlertDialog(context, "alert message"), | |
child: new Text("Alert Dialog")) | |
], | |
), | |
), | |
floatingActionButton: new FloatingActionButton( | |
mini: true, | |
backgroundColor: Colors.deepPurple, | |
onPressed: _onActionButton, | |
child: new Icon(Icons.access_alarm) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment