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 (single-file apps)
Convert 20 more single-file apps to the validated pattern: lib/main.dart holds only the runApp + MaterialApp bootstrap, while a new lib/example.dart holds the full screen (Scaffold, AppBar) and the focused example code. Apps: enabling_splash_screen, getx_counter_app, image_from_network, infinite_list, load_local_image, load_local_json, persist_key_value, push_notifications, sliver_app_bar_example, stateless_widgets, tic_tac_toe, tip_calculator, using_alert_dialog, using_edittext, using_expansionpanel, using_http_get, using_interactiveviewer, using_snackbar, using_stepper, using_theme
This commit is contained in:
87
stateless_widgets/lib/example.dart
Normal file
87
stateless_widgets/lib/example.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Example: build a layout from reusable stateless widgets.
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Declare some constants
|
||||
final double myTextSize = 30.0;
|
||||
final double myIconSize = 40.0;
|
||||
final TextStyle myTextStyle =
|
||||
TextStyle(color: Colors.grey, fontSize: myTextSize);
|
||||
|
||||
var column = Column(
|
||||
// Makes the cards stretch in horizontal axis
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
// Setup the card
|
||||
MyCard(
|
||||
// Setup the text
|
||||
title: Text(
|
||||
"Favorite",
|
||||
style: myTextStyle,
|
||||
),
|
||||
// Setup the icon
|
||||
icon: Icon(Icons.favorite, size: myIconSize, color: Colors.red)),
|
||||
MyCard(
|
||||
title: Text(
|
||||
"Alarm",
|
||||
style: myTextStyle,
|
||||
),
|
||||
icon: Icon(Icons.alarm, size: myIconSize, color: Colors.blue)),
|
||||
MyCard(
|
||||
title: Text(
|
||||
"Airport Shuttle",
|
||||
style: myTextStyle,
|
||||
),
|
||||
icon: Icon(Icons.airport_shuttle,
|
||||
size: myIconSize, color: Colors.amber)),
|
||||
MyCard(
|
||||
title: Text(
|
||||
"Done",
|
||||
style: myTextStyle,
|
||||
),
|
||||
icon: Icon(Icons.done, size: myIconSize, color: Colors.green)),
|
||||
],
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Stateless Widget"),
|
||||
),
|
||||
body: Container(
|
||||
// Sets the padding in the main container
|
||||
padding: const EdgeInsets.only(bottom: 2.0),
|
||||
child: Center(
|
||||
child: SingleChildScrollView(child: column),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a reusable stateless widget
|
||||
class MyCard extends StatelessWidget {
|
||||
final Widget icon;
|
||||
final Widget title;
|
||||
|
||||
// Constructor. {} here denote that they are optional values i.e you can use as: MyCard()
|
||||
const MyCard({super.key, required this.title, required this.icon});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.only(bottom: 1.0),
|
||||
child: Card(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
children: <Widget>[title, icon],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: MyApp(),
|
||||
// Define the theme, set the primary swatch
|
||||
theme: ThemeData(primarySwatch: Colors.green),
|
||||
));
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@@ -13,82 +11,11 @@ class MyApp extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Declare some constants
|
||||
final double myTextSize = 30.0;
|
||||
final double myIconSize = 40.0;
|
||||
final TextStyle myTextStyle =
|
||||
TextStyle(color: Colors.grey, fontSize: myTextSize);
|
||||
|
||||
var column = Column(
|
||||
// Makes the cards stretch in horizontal axis
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
// Setup the card
|
||||
MyCard(
|
||||
// Setup the text
|
||||
title: Text(
|
||||
"Favorite",
|
||||
style: myTextStyle,
|
||||
),
|
||||
// Setup the icon
|
||||
icon: Icon(Icons.favorite, size: myIconSize, color: Colors.red)),
|
||||
MyCard(
|
||||
title: Text(
|
||||
"Alarm",
|
||||
style: myTextStyle,
|
||||
),
|
||||
icon: Icon(Icons.alarm, size: myIconSize, color: Colors.blue)),
|
||||
MyCard(
|
||||
title: Text(
|
||||
"Airport Shuttle",
|
||||
style: myTextStyle,
|
||||
),
|
||||
icon: Icon(Icons.airport_shuttle,
|
||||
size: myIconSize, color: Colors.amber)),
|
||||
MyCard(
|
||||
title: Text(
|
||||
"Done",
|
||||
style: myTextStyle,
|
||||
),
|
||||
icon: Icon(Icons.done, size: myIconSize, color: Colors.green)),
|
||||
],
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Stateless Widget"),
|
||||
),
|
||||
body: Container(
|
||||
// Sets the padding in the main container
|
||||
padding: const EdgeInsets.only(bottom: 2.0),
|
||||
child: Center(
|
||||
child: SingleChildScrollView(child: column),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a reusable stateless widget
|
||||
class MyCard extends StatelessWidget {
|
||||
final Widget icon;
|
||||
final Widget title;
|
||||
|
||||
// Constructor. {} here denote that they are optional values i.e you can use as: MyCard()
|
||||
const MyCard({super.key, required this.title, required this.icon});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.only(bottom: 1.0),
|
||||
child: Card(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
children: <Widget>[title, icon],
|
||||
),
|
||||
),
|
||||
),
|
||||
return MaterialApp(
|
||||
title: "Stateless Widget",
|
||||
// Define the theme, set the primary swatch
|
||||
theme: ThemeData(primarySwatch: Colors.green),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user