Skip to content

Commit

Permalink
[material_3_demo] Refactor application code into multiple small libra…
Browse files Browse the repository at this point in the history
…ries (#2581)
  • Loading branch information
kevmoo authored Feb 18, 2025
1 parent a8d02fb commit 6cf8189
Show file tree
Hide file tree
Showing 23 changed files with 1,000 additions and 910 deletions.
863 changes: 0 additions & 863 deletions material_3_demo/lib/home.dart

This file was deleted.

78 changes: 39 additions & 39 deletions material_3_demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import 'package:flutter/material.dart';

import 'constants.dart';
import 'home.dart';
import 'src/constants.dart';
import 'src/home.dart';

void main() async {
runApp(const App());
Expand All @@ -19,49 +19,49 @@ class App extends StatefulWidget {
}

class _AppState extends State<App> {
bool useMaterial3 = true;
ThemeMode themeMode = ThemeMode.system;
ColorSeed colorSelected = ColorSeed.baseColor;
ColorImageProvider imageSelected = ColorImageProvider.leaves;
ColorScheme? imageColorScheme = const ColorScheme.light();
ColorSelectionMethod colorSelectionMethod = ColorSelectionMethod.colorSeed;
bool _useMaterial3 = true;
ThemeMode _themeMode = ThemeMode.system;
ColorSeed _colorSelected = ColorSeed.baseColor;
ColorImageProvider _imageSelected = ColorImageProvider.leaves;
ColorScheme? _imageColorScheme = const ColorScheme.light();
ColorSelectionMethod _colorSelectionMethod = ColorSelectionMethod.colorSeed;

bool get useLightMode => switch (themeMode) {
bool get _useLightMode => switch (_themeMode) {
ThemeMode.system =>
View.of(context).platformDispatcher.platformBrightness ==
Brightness.light,
ThemeMode.light => true,
ThemeMode.dark => false,
};

void handleBrightnessChange(bool useLightMode) {
void _handleBrightnessChange(bool useLightMode) {
setState(() {
themeMode = useLightMode ? ThemeMode.light : ThemeMode.dark;
_themeMode = useLightMode ? ThemeMode.light : ThemeMode.dark;
});
}

void handleMaterialVersionChange() {
void _handleMaterialVersionChange() {
setState(() {
useMaterial3 = !useMaterial3;
_useMaterial3 = !_useMaterial3;
});
}

void handleColorSelect(int value) {
void _handleColorSelect(int value) {
setState(() {
colorSelectionMethod = ColorSelectionMethod.colorSeed;
colorSelected = ColorSeed.values[value];
_colorSelectionMethod = ColorSelectionMethod.colorSeed;
_colorSelected = ColorSeed.values[value];
});
}

void handleImageSelect(int value) {
void _handleImageSelect(int value) {
final String url = ColorImageProvider.values[value].url;
ColorScheme.fromImageProvider(provider: NetworkImage(url)).then((
newScheme,
) {
setState(() {
colorSelectionMethod = ColorSelectionMethod.image;
imageSelected = ColorImageProvider.values[value];
imageColorScheme = newScheme;
_colorSelectionMethod = ColorSelectionMethod.image;
_imageSelected = ColorImageProvider.values[value];
_imageColorScheme = newScheme;
});
});
}
Expand All @@ -71,37 +71,37 @@ class _AppState extends State<App> {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Material 3',
themeMode: themeMode,
themeMode: _themeMode,
theme: ThemeData(
colorSchemeSeed:
colorSelectionMethod == ColorSelectionMethod.colorSeed
? colorSelected.color
_colorSelectionMethod == ColorSelectionMethod.colorSeed
? _colorSelected.color
: null,
colorScheme:
colorSelectionMethod == ColorSelectionMethod.image
? imageColorScheme
_colorSelectionMethod == ColorSelectionMethod.image
? _imageColorScheme
: null,
useMaterial3: useMaterial3,
useMaterial3: _useMaterial3,
brightness: Brightness.light,
),
darkTheme: ThemeData(
colorSchemeSeed:
colorSelectionMethod == ColorSelectionMethod.colorSeed
? colorSelected.color
: imageColorScheme!.primary,
useMaterial3: useMaterial3,
_colorSelectionMethod == ColorSelectionMethod.colorSeed
? _colorSelected.color
: _imageColorScheme!.primary,
useMaterial3: _useMaterial3,
brightness: Brightness.dark,
),
home: Home(
useLightMode: useLightMode,
useMaterial3: useMaterial3,
colorSelected: colorSelected,
imageSelected: imageSelected,
handleBrightnessChange: handleBrightnessChange,
handleMaterialVersionChange: handleMaterialVersionChange,
handleColorSelect: handleColorSelect,
handleImageSelect: handleImageSelect,
colorSelectionMethod: colorSelectionMethod,
useLightMode: _useLightMode,
useMaterial3: _useMaterial3,
colorSelected: _colorSelected,
imageSelected: _imageSelected,
handleBrightnessChange: _handleBrightnessChange,
handleMaterialVersionChange: _handleMaterialVersionChange,
handleColorSelect: _handleColorSelect,
handleImageSelect: _handleImageSelect,
colorSelectionMethod: _colorSelectionMethod,
),
);
}
Expand Down
31 changes: 31 additions & 0 deletions material_3_demo/lib/src/animations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

class SizeAnimation extends CurvedAnimation {
SizeAnimation(Animation<double> parent)
: super(
parent: parent,
curve: const Interval(0.2, 0.8, curve: Curves.easeInOutCubicEmphasized),
reverseCurve: Interval(
0,
0.2,
curve: Curves.easeInOutCubicEmphasized.flipped,
),
);
}

class OffsetAnimation extends CurvedAnimation {
OffsetAnimation(Animation<double> parent)
: super(
parent: parent,
curve: const Interval(0.4, 1.0, curve: Curves.easeInOutCubicEmphasized),
reverseCurve: Interval(
0,
0.2,
curve: Curves.easeInOutCubicEmphasized.flipped,
),
);
}
59 changes: 59 additions & 0 deletions material_3_demo/lib/src/bar_transition.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';

import 'animations.dart';

class BarTransition extends StatefulWidget {
const BarTransition({
super.key,
required this.animation,
required this.backgroundColor,
required this.child,
});

final Animation<double> animation;
final Color backgroundColor;
final Widget child;

@override
State<BarTransition> createState() => _BarTransition();
}

class _BarTransition extends State<BarTransition> {
late final Animation<Offset> offsetAnimation;
late final Animation<double> heightAnimation;

@override
void initState() {
super.initState();

offsetAnimation = Tween<Offset>(
begin: const Offset(0, 1),
end: Offset.zero,
).animate(OffsetAnimation(widget.animation));

heightAnimation = Tween<double>(
begin: 0,
end: 1,
).animate(SizeAnimation(widget.animation));
}

@override
Widget build(BuildContext context) {
return ClipRect(
child: DecoratedBox(
decoration: BoxDecoration(color: widget.backgroundColor),
child: Align(
alignment: Alignment.topLeft,
heightFactor: heightAnimation.value,
child: FractionalTranslation(
translation: offsetAnimation.value,
child: widget.child,
),
),
),
);
}
}
Loading

0 comments on commit 6cf8189

Please sign in to comment.