mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
refactor: split app wrapper from focused example code (pilot)
Apply a consistent structure to 5 example apps (simple_material_app, stateful_widget, dropdown_button, using_gradient, using_tabs): - lib/main.dart now contains only the app bootstrap: runApp + a const MyApp widget wrapping the app in a MaterialApp. - lib/example.dart holds the full screen (Scaffold, AppBar, and the focused code that demonstrates the example feature), marked with a '// Example:' banner comment. This makes it easy to pinpoint the code that teaches each concept while keeping the wrapping/chrome code uniform and minimal. Review this pilot before rolling the pattern out to the remaining apps.
This commit is contained in:
53
stateful_widget/lib/example.dart
Normal file
53
stateful_widget/lib/example.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Example: the focused code for this app.
|
||||
// A StatefulWidget that cycles through a list of strings each time the
|
||||
// button is pressed.
|
||||
class Example extends StatefulWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
State<Example> createState() => _ExampleState();
|
||||
}
|
||||
|
||||
class _ExampleState extends State<Example> {
|
||||
int counter = 0;
|
||||
List<String> strings = ['Flutter', 'is', 'cool', "and", "awesome!"];
|
||||
String displayedString = "Hello World!";
|
||||
|
||||
void onPressOfButton() {
|
||||
setState(() {
|
||||
displayedString = strings[counter];
|
||||
counter = counter < 4 ? counter + 1 : 0;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Stateful Widget"),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
body: Container(
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(displayedString, style: TextStyle(fontSize: 40.0)),
|
||||
Padding(padding: EdgeInsets.all(10.0)),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
||||
onPressed: onPressOfButton,
|
||||
child: Text(
|
||||
"Press me",
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,58 +1,19 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: MyButton(),
|
||||
));
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyButton extends StatefulWidget {
|
||||
const MyButton({super.key});
|
||||
|
||||
@override
|
||||
MyButtonState createState() {
|
||||
return MyButtonState();
|
||||
}
|
||||
}
|
||||
|
||||
class MyButtonState extends State<MyButton> {
|
||||
int counter = 0;
|
||||
List<String> strings = ['Flutter', 'is', 'cool', "and", "awesome!"];
|
||||
String displayedString = "Hello World!";
|
||||
|
||||
void onPressOfButton() {
|
||||
setState(() {
|
||||
displayedString = strings[counter];
|
||||
counter = counter < 4 ? counter + 1 : 0;
|
||||
});
|
||||
}
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Stateful Widget"),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
body: Container(
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(displayedString, style: TextStyle(fontSize: 40.0)),
|
||||
Padding(padding: EdgeInsets.all(10.0)),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
||||
onPressed: onPressOfButton,
|
||||
child: Text(
|
||||
"Press me",
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
return MaterialApp(
|
||||
title: "Stateful Widget",
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user