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:
45
using_theme/lib/example.dart
Normal file
45
using_theme/lib/example.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Example: use the app theme to style widgets and override it locally.
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
// AppBar
|
||||
appBar: AppBar(
|
||||
// AppBar Title
|
||||
title: const Text("Using Theme"),
|
||||
),
|
||||
body: Container(
|
||||
// Another way to set the background color
|
||||
decoration: const BoxDecoration(color: Colors.black87),
|
||||
child: Center(
|
||||
child: Container(
|
||||
// use the theme accent color as background color for this widget
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
child: Text(
|
||||
'Hello World!',
|
||||
// Set text style as per theme
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
floatingActionButton: Theme(
|
||||
// override the accent color of theme for this widget only
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: Theme.of(context)
|
||||
.colorScheme
|
||||
.copyWith(secondary: Colors.pinkAccent),
|
||||
),
|
||||
child: FloatingActionButton(
|
||||
onPressed: null,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +1,28 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: MyHome(),
|
||||
// Set the theme's primary color, accent color,
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.green,
|
||||
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.green)
|
||||
.copyWith(secondary: Colors.lightGreenAccent),
|
||||
// Set background color
|
||||
scaffoldBackgroundColor: Colors.black12,
|
||||
),
|
||||
));
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyHome extends StatelessWidget {
|
||||
const MyHome({super.key});
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
// AppBar
|
||||
appBar: AppBar(
|
||||
// AppBar Title
|
||||
title: Text("Using Theme"),
|
||||
),
|
||||
body: Container(
|
||||
// Another way to set the background color
|
||||
decoration: BoxDecoration(color: Colors.black87),
|
||||
child: Center(
|
||||
child: Container(
|
||||
// use the theme accent color as background color for this widget
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
child: Text(
|
||||
'Hello World!',
|
||||
// Set text style as per theme
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
floatingActionButton: Theme(
|
||||
// override the accent color of theme for this widget only
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: Theme.of(context)
|
||||
.colorScheme
|
||||
.copyWith(secondary: Colors.pinkAccent),
|
||||
),
|
||||
child: FloatingActionButton(
|
||||
onPressed: null,
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: "Using Theme",
|
||||
// Set the theme's primary color, accent color,
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.green,
|
||||
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.green)
|
||||
.copyWith(secondary: Colors.lightGreenAccent),
|
||||
// Set background color
|
||||
scaffoldBackgroundColor: Colors.black12,
|
||||
),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user