Skip to content

Instantly share code, notes, and snippets.

@sethladd
Last active February 3, 2025 05:44
Show Gist options
  • Save sethladd/1cca38ed9aaec097dffcc0570886a7a0 to your computer and use it in GitHub Desktop.
Save sethladd/1cca38ed9aaec097dffcc0570886a7a0 to your computer and use it in GitHub Desktop.
Wild ideas
//Before:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stepper'),
),
body: Stepper(
steps: widget.steps,
currentStep: widget.currentStep,
onStepTapped: widget.onStepTapped,
onStepContinue: widget.onStepContinue,
onStepCancel: widget.onStepCancel,
controlsBuilder: (BuildContext context, ControlsDetails details) {
return Row(
children: <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: details.onStepCancel,
),
TextButton(
child: Text('Continue'),
onPressed: details.onStepContinue,
),
],
);
},
),
);
}
//After:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar( title: 'Stepper' /* turn a String into a Text */ ),
body: Stepper(
steps: widget.steps,
currentStep: widget.currentStep,
onStepTapped: widget.onStepTapped,
onStepContinue: widget.onStepContinue,
onStepCancel: widget.onStepCancel,
controlsBuilder: (BuildContext context, ControlsDetails details) {
Row() {
TextButton(onPressed: details.onStepCancel) { 'Cancel' },
TextButton(onPressed: details.onStepContinue) { 'Continue' }
}
},
),
);
}
// after, with the pipe-like operator, and the "strings can be Text" hypothetical
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: 'Stepper'),
body: Stepper(
steps: widget.steps,
currentStep: widget.currentStep,
onStepTapped: widget.onStepTapped,
onStepContinue: widget.onStepContinue,
onStepCancel: widget.onStepCancel,
controlsBuilder: (BuildContext context, ControlsDetails details) {
return Row()
|> [
TextButton(onPressed: details.onStepCancel)
|> 'Cancel',
TextButton(onPressed: details.onStepContinue)
|> 'Continue',
];
},
),
);
}
//Before:
Text(
'No, we need bold strokes. We need this plan.',
style: TextStyle(fontWeight: FontWeight.bold),
)
//After: (here, we assume we know that style: wants a TextStyle,
//so we don't need to repeat the constructor name. We also assume
//the enum shorthand)
Text(
'No, we need bold strokes. We need this plan.',
style: (fontWeight: .bold),
)
// Before
import 'package:flutter/material.dart';
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _counter = 0;
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _increment,
child: const Text('Increment'),
),
const SizedBox(width: 16),
Text('Count: $_counter'),
],
);
}
}
void main() {
runApp(
const MaterialApp(
home: Scaffold(
body: Center(
child: Counter(),
),
),
),
);
}
//After:
import 'package:flutter/material.dart';
class Counter extends Widget {
const Counter({super.key});
@state int _counter = 0;
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
Row(mainAxisAlignment: .center) {
ElevatedButton(onPressed: _increment) {
Text('Increment')
}
const SizedBox(width: 16),
Text('Count: $_counter')
}
}
}
void main() {
runApp(
const MaterialApp() {
Scaffold() {
Center() {
Counter()
}
}
}
);
}
// or, if you really want to get nasty
import package:flutter.material
class Counter({super.key}) extends Widget {
@state int _counter = 0
@setState void _increment() {
_counter++
}
@override
Widget build(BuildContext context) {
Row(mainAxisAlignment: .center)
|> [
ElevatedButton(onPressed: _increment)
|> Text('Increment')
SizedBox(width: 16)
Text('Count: $_counter')
]
}
}
void main() {
runApp(
MaterialApp()
|> Scaffold()
|> Center()
|> Counter()
)
}
So, build() is on State (not a widget) and StatelessWidget.
Is there a way to have a single widget subclass, with a build() off of that?
//Before:
Step(
title: Text('Shipping', style: TextStyle(fontWeight: FontWeight.bold)),
content: Column(
children: [
TextField(label: Text('Address')),
TextField(label: Text('City')),
TextField(label: Text('Zip Code')),
],
),
)
//After:
Step(title: Text('Shipping', style: (fontWeight: .bold)) {
Column() {
TextField(label: 'Address'),
TextField(label: 'City'),
TextField(label: 'Zip Code')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment