Skip to content

Commit 25d9bab

Browse files
committed
[material_3_demo] Refactor application code into multiple small libraries
1 parent 719fd72 commit 25d9bab

23 files changed

+999
-909
lines changed

material_3_demo/lib/home.dart

-863
This file was deleted.

material_3_demo/lib/main.dart

+39-39
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import 'package:flutter/material.dart';
66

7-
import 'constants.dart';
8-
import 'home.dart';
7+
import 'src/constants.dart';
8+
import 'src/home.dart';
99

1010
void main() async {
1111
runApp(const App());
@@ -19,49 +19,49 @@ class App extends StatefulWidget {
1919
}
2020

2121
class _AppState extends State<App> {
22-
bool useMaterial3 = true;
23-
ThemeMode themeMode = ThemeMode.system;
24-
ColorSeed colorSelected = ColorSeed.baseColor;
25-
ColorImageProvider imageSelected = ColorImageProvider.leaves;
26-
ColorScheme? imageColorScheme = const ColorScheme.light();
27-
ColorSelectionMethod colorSelectionMethod = ColorSelectionMethod.colorSeed;
22+
bool _useMaterial3 = true;
23+
ThemeMode _themeMode = ThemeMode.system;
24+
ColorSeed _colorSelected = ColorSeed.baseColor;
25+
ColorImageProvider _imageSelected = ColorImageProvider.leaves;
26+
ColorScheme? _imageColorScheme = const ColorScheme.light();
27+
ColorSelectionMethod _colorSelectionMethod = ColorSelectionMethod.colorSeed;
2828

29-
bool get useLightMode => switch (themeMode) {
29+
bool get _useLightMode => switch (_themeMode) {
3030
ThemeMode.system =>
3131
View.of(context).platformDispatcher.platformBrightness ==
3232
Brightness.light,
3333
ThemeMode.light => true,
3434
ThemeMode.dark => false,
3535
};
3636

37-
void handleBrightnessChange(bool useLightMode) {
37+
void _handleBrightnessChange(bool useLightMode) {
3838
setState(() {
39-
themeMode = useLightMode ? ThemeMode.light : ThemeMode.dark;
39+
_themeMode = useLightMode ? ThemeMode.light : ThemeMode.dark;
4040
});
4141
}
4242

43-
void handleMaterialVersionChange() {
43+
void _handleMaterialVersionChange() {
4444
setState(() {
45-
useMaterial3 = !useMaterial3;
45+
_useMaterial3 = !_useMaterial3;
4646
});
4747
}
4848

49-
void handleColorSelect(int value) {
49+
void _handleColorSelect(int value) {
5050
setState(() {
51-
colorSelectionMethod = ColorSelectionMethod.colorSeed;
52-
colorSelected = ColorSeed.values[value];
51+
_colorSelectionMethod = ColorSelectionMethod.colorSeed;
52+
_colorSelected = ColorSeed.values[value];
5353
});
5454
}
5555

56-
void handleImageSelect(int value) {
56+
void _handleImageSelect(int value) {
5757
final String url = ColorImageProvider.values[value].url;
5858
ColorScheme.fromImageProvider(provider: NetworkImage(url)).then((
5959
newScheme,
6060
) {
6161
setState(() {
62-
colorSelectionMethod = ColorSelectionMethod.image;
63-
imageSelected = ColorImageProvider.values[value];
64-
imageColorScheme = newScheme;
62+
_colorSelectionMethod = ColorSelectionMethod.image;
63+
_imageSelected = ColorImageProvider.values[value];
64+
_imageColorScheme = newScheme;
6565
});
6666
});
6767
}
@@ -71,37 +71,37 @@ class _AppState extends State<App> {
7171
return MaterialApp(
7272
debugShowCheckedModeBanner: false,
7373
title: 'Material 3',
74-
themeMode: themeMode,
74+
themeMode: _themeMode,
7575
theme: ThemeData(
7676
colorSchemeSeed:
77-
colorSelectionMethod == ColorSelectionMethod.colorSeed
78-
? colorSelected.color
77+
_colorSelectionMethod == ColorSelectionMethod.colorSeed
78+
? _colorSelected.color
7979
: null,
8080
colorScheme:
81-
colorSelectionMethod == ColorSelectionMethod.image
82-
? imageColorScheme
81+
_colorSelectionMethod == ColorSelectionMethod.image
82+
? _imageColorScheme
8383
: null,
84-
useMaterial3: useMaterial3,
84+
useMaterial3: _useMaterial3,
8585
brightness: Brightness.light,
8686
),
8787
darkTheme: ThemeData(
8888
colorSchemeSeed:
89-
colorSelectionMethod == ColorSelectionMethod.colorSeed
90-
? colorSelected.color
91-
: imageColorScheme!.primary,
92-
useMaterial3: useMaterial3,
89+
_colorSelectionMethod == ColorSelectionMethod.colorSeed
90+
? _colorSelected.color
91+
: _imageColorScheme!.primary,
92+
useMaterial3: _useMaterial3,
9393
brightness: Brightness.dark,
9494
),
9595
home: Home(
96-
useLightMode: useLightMode,
97-
useMaterial3: useMaterial3,
98-
colorSelected: colorSelected,
99-
imageSelected: imageSelected,
100-
handleBrightnessChange: handleBrightnessChange,
101-
handleMaterialVersionChange: handleMaterialVersionChange,
102-
handleColorSelect: handleColorSelect,
103-
handleImageSelect: handleImageSelect,
104-
colorSelectionMethod: colorSelectionMethod,
96+
useLightMode: _useLightMode,
97+
useMaterial3: _useMaterial3,
98+
colorSelected: _colorSelected,
99+
imageSelected: _imageSelected,
100+
handleBrightnessChange: _handleBrightnessChange,
101+
handleMaterialVersionChange: _handleMaterialVersionChange,
102+
handleColorSelect: _handleColorSelect,
103+
handleImageSelect: _handleImageSelect,
104+
colorSelectionMethod: _colorSelectionMethod,
105105
),
106106
);
107107
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 The Flutter team. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
7+
class SizeAnimation extends CurvedAnimation {
8+
SizeAnimation(Animation<double> parent)
9+
: super(
10+
parent: parent,
11+
curve: const Interval(0.2, 0.8, curve: Curves.easeInOutCubicEmphasized),
12+
reverseCurve: Interval(
13+
0,
14+
0.2,
15+
curve: Curves.easeInOutCubicEmphasized.flipped,
16+
),
17+
);
18+
}
19+
20+
class OffsetAnimation extends CurvedAnimation {
21+
OffsetAnimation(Animation<double> parent)
22+
: super(
23+
parent: parent,
24+
curve: const Interval(0.4, 1.0, curve: Curves.easeInOutCubicEmphasized),
25+
reverseCurve: Interval(
26+
0,
27+
0.2,
28+
curve: Curves.easeInOutCubicEmphasized.flipped,
29+
),
30+
);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 The Flutter team. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
import 'package:flutter/widgets.dart';
5+
6+
import 'animations.dart';
7+
8+
class BarTransition extends StatefulWidget {
9+
const BarTransition({
10+
super.key,
11+
required this.animation,
12+
required this.backgroundColor,
13+
required this.child,
14+
});
15+
16+
final Animation<double> animation;
17+
final Color backgroundColor;
18+
final Widget child;
19+
20+
@override
21+
State<BarTransition> createState() => _BarTransition();
22+
}
23+
24+
class _BarTransition extends State<BarTransition> {
25+
late final Animation<Offset> offsetAnimation;
26+
late final Animation<double> heightAnimation;
27+
28+
@override
29+
void initState() {
30+
super.initState();
31+
32+
offsetAnimation = Tween<Offset>(
33+
begin: const Offset(0, 1),
34+
end: Offset.zero,
35+
).animate(OffsetAnimation(widget.animation));
36+
37+
heightAnimation = Tween<double>(
38+
begin: 0,
39+
end: 1,
40+
).animate(SizeAnimation(widget.animation));
41+
}
42+
43+
@override
44+
Widget build(BuildContext context) {
45+
return ClipRect(
46+
child: DecoratedBox(
47+
decoration: BoxDecoration(color: widget.backgroundColor),
48+
child: Align(
49+
alignment: Alignment.topLeft,
50+
heightFactor: heightAnimation.value,
51+
child: FractionalTranslation(
52+
translation: offsetAnimation.value,
53+
child: widget.child,
54+
),
55+
),
56+
),
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)