Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
refactor: split widgets in different files (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiMiguel authored Dec 12, 2023
1 parent ab9ad69 commit 6af4b96
Show file tree
Hide file tree
Showing 14 changed files with 570 additions and 512 deletions.
310 changes: 0 additions & 310 deletions lib/results/view/results_view.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:api_client/api_client.dart';
import 'package:app_ui/app_ui.dart';
import 'package:dash_ai_search/home/home.dart';
import 'package:dash_ai_search/l10n/l10n.dart';
import 'package:dash_ai_search/question/question.dart';
import 'package:dash_ai_search/results/results.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down Expand Up @@ -104,67 +102,6 @@ class _ResultsView extends StatelessWidget {
}
}

class SearchBoxView extends StatefulWidget {
@visibleForTesting
const SearchBoxView({super.key});

@override
State<SearchBoxView> createState() => SearchBoxViewState();
}

class SearchBoxViewState extends State<SearchBoxView>
with TickerProviderStateMixin, TransitionScreenMixin {
late Animation<Offset> _offset;
late Animation<double> _opacity;

@override
List<Status> get forwardEnterStatuses => [Status.thinkingToResults];

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

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);

exitTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
}

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

_offset = Tween<Offset>(begin: const Offset(0, 1), end: Offset.zero)
.animate(enterTransitionController);

_opacity =
Tween<double>(begin: 0, end: 1).animate(enterTransitionController);
}

@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints(
maxWidth: 659,
),
child: SlideTransition(
position: _offset,
child: FadeTransition(
opacity: _opacity,
child: const SearchBox(
askAgain: true,
),
),
),
);
}
}

class BlueContainer extends StatefulWidget {
@visibleForTesting
const BlueContainer({
Expand Down Expand Up @@ -451,126 +388,6 @@ class _AiResponseState extends State<_AiResponse>
}
}

class SummaryView extends StatefulWidget {
const SummaryView({
super.key,
});

@override
State<SummaryView> createState() => _SummaryViewState();
}

class _SummaryViewState extends State<SummaryView>
with TickerProviderStateMixin, TransitionScreenMixin {
late Animation<double> _width;

@override
List<Status> get forwardExitStatuses => [Status.resultsToSourceAnswers];

@override
List<Status> get backEnterStatuses => [Status.sourceAnswersBackToResults];

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

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);

exitTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
}

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

_width = Tween<double>(begin: 563, end: 659).animate(
CurvedAnimation(
parent: exitTransitionController,
curve: Curves.decelerate,
),
);
}

@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
final parsed = context.select((HomeBloc bloc) => bloc.state.parsedSummary);

return AnimatedBuilder(
animation: _width,
builder: (context, child) {
return SizedBox(
width: _width.value,
child: RichText(
text: TextSpan(
children: [
for (final element in parsed.elements)
if (element.isLink)
WidgetSpan(
child: InkWell(
onTap: () {
final isOnSeeSourceAnswers =
context.read<HomeBloc>().state.status ==
Status.seeSourceAnswers;
if (isOnSeeSourceAnswers) {
context.read<HomeBloc>().add(
HomeSourceAnswersNavigated(
element.text,
),
);
} else {
context.read<HomeBloc>().add(
HomeSeeSourceAnswersRequested(
element.text,
),
);
}
},
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 2,
),
padding: const EdgeInsets.symmetric(
vertical: 4,
horizontal: 12,
),
decoration: const BoxDecoration(
color: VertexColors.white,
borderRadius: BorderRadius.all(
Radius.circular(100),
),
),
child: Text(
element.text,
style: textTheme.labelLarge?.copyWith(
color: VertexColors.googleBlue,
),
),
),
),
)
else
TextSpan(
text: element.text,
style: textTheme.headlineLarge?.copyWith(
color: VertexColors.white,
),
),
],
),
),
);
},
);
}
}

class CarouselView extends StatefulWidget {
@visibleForTesting
const CarouselView({
Expand Down Expand Up @@ -645,130 +462,3 @@ class CarouselViewState extends State<CarouselView>
);
}
}

class BackToAnswerButton extends StatefulWidget {
@visibleForTesting
const BackToAnswerButton({super.key});

@override
State<BackToAnswerButton> createState() => _BackToAnswerButtonState();
}

class _BackToAnswerButtonState extends State<BackToAnswerButton>
with TickerProviderStateMixin, TransitionScreenMixin {
late Animation<double> _sizeExitIn;

@override
List<Status> get forwardExitStatuses => [Status.resultsToSourceAnswers];

@override
List<Status> get backEnterStatuses => [Status.sourceAnswersBackToResults];

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

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);

exitTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
}

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

_sizeExitIn = CurvedAnimation(
parent: exitTransitionController,
curve: Curves.decelerate,
);
}

@override
Widget build(BuildContext context) {
final l10n = context.l10n;

return SizeTransition(
sizeFactor: _sizeExitIn,
axis: Axis.horizontal,
child: Align(
alignment: Alignment.topLeft,
child: TertiaryCTA(
key: const Key('backToAnswerButtonKey'),
label: l10n.backToAIAnswer,
icon: vertexIcons.arrowBack.image(color: VertexColors.white),
onPressed: () {
context.read<HomeBloc>().add(const HomeBackToAiSummaryTapped());
},
),
),
);
}
}

class SeeSourceAnswersButton extends StatefulWidget {
@visibleForTesting
const SeeSourceAnswersButton({super.key});

@override
State<SeeSourceAnswersButton> createState() => _SeeSourceAnswersButtonState();
}

class _SeeSourceAnswersButtonState extends State<SeeSourceAnswersButton>
with TickerProviderStateMixin, TransitionScreenMixin {
late Animation<double> _opacityExitOut;

@override
List<Status> get forwardExitStatuses => [Status.resultsToSourceAnswers];

@override
List<Status> get backEnterStatuses => [Status.sourceAnswersBackToResults];

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

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);

exitTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
}

@override
void initState() {
super.initState();
_opacityExitOut =
Tween<double>(begin: 1, end: 0).animate(exitTransitionController);
}

@override
Widget build(BuildContext context) {
final l10n = context.l10n;

return FadeTransition(
opacity: _opacityExitOut,
child: Align(
alignment: Alignment.bottomRight,
child: TertiaryCTA(
label: l10n.seeSourceAnswers,
icon: vertexIcons.arrowForward.image(
color: VertexColors.white,
),
onPressed: () => context
.read<HomeBloc>()
.add(const HomeSeeSourceAnswersRequested(null)),
),
),
);
}
}
Loading

0 comments on commit 6af4b96

Please sign in to comment.