Skip to content

Instantly share code, notes, and snippets.

@kitsuniru
Created July 8, 2024 13:03
Show Gist options
  • Save kitsuniru/79dff07bfff42281ac72b44eeecf12e3 to your computer and use it in GitHub Desktop.
Save kitsuniru/79dff07bfff42281ac72b44eeecf12e3 to your computer and use it in GitHub Desktop.
layout widget switching problem
// Copyright 2019 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
super.key,
required this.title,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
enum ExampleState{
loading,
error,
content,
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
final ValueNotifier<ExampleState> notifier = ValueNotifier(ExampleState.loading);
late final Timer _timer;
void initState(){
super.initState();
_timer = Timer.periodic(Duration(seconds: 3), (_) {
final value = notifier.value;
notifier.value = switch (value) {
ExampleState.loading => ExampleState.content,
ExampleState.content => ExampleState.error,
ExampleState.error => ExampleState.loading,
};
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ListenableBuilder(
listenable: notifier,
builder: (context, _) => switch (notifier.value) {
ExampleState.loading => ExampleLayoutWidget.loading(),
ExampleState.content => ExampleLayoutWidget.content(),
ExampleState.error => ExampleLayoutWidget.error(failure: Object()),
},
)
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
abstract class ExampleLayoutWidget extends StatefulWidget {
const ExampleLayoutWidget._({
super.key,
});
factory ExampleLayoutWidget.loading({
Key? key,
}) =>
_ExampleLayoutWidgetLoading._(
key: key,
);
factory ExampleLayoutWidget.error({
required Object failure,
Key? key,
}) =>
_ExampleLayoutWidgetError._(
failure: failure,
key: key,
);
factory ExampleLayoutWidget.content() => _ExampleLayoutWidgetContent._();
}
class _ExampleLayoutWidgetError extends ExampleLayoutWidget {
const _ExampleLayoutWidgetError._({
required Object failure,
super.key,
}) : super._();
@override
State<_ExampleLayoutWidgetError> createState() => _ExampleLayoutWidgetErrorState();
}
class _ExampleLayoutWidgetErrorState extends State<_ExampleLayoutWidgetError> {
@override
Widget build(BuildContext context) {
return ColorBoxy(child: Center(
child: Text('error'),
));
}
}
class _ExampleLayoutWidgetContent extends ExampleLayoutWidget {
const _ExampleLayoutWidgetContent._({
super.key,
}) : super._();
@override
State<_ExampleLayoutWidgetContent> createState() => _ExampleLayoutWidgetContentState();
}
class _ExampleLayoutWidgetContentState extends State<_ExampleLayoutWidgetContent> {
@override
Widget build(BuildContext context) {
return ColorBoxy(child: const Center(
child: Text('done'),
));
}
}
class _ExampleLayoutWidgetLoading extends ExampleLayoutWidget {
const _ExampleLayoutWidgetLoading._({
super.key,
}) : super._();
@override
State<_ExampleLayoutWidgetLoading> createState() => _ExampleLayoutWidgetLoadingState();
}
class _ExampleLayoutWidgetLoadingState extends State<_ExampleLayoutWidgetLoading> {
@override
Widget build(BuildContext context) {
return ColorBoxy(child: const Center(
child: CircularProgressIndicator(),
));
}
}
class ColorBoxy extends StatefulWidget {
const ColorBoxy({super.key, required this.child,});
final Widget child;
@override
State<ColorBoxy> createState() => _ColorBoxyState();
}
class _ColorBoxyState extends State<ColorBoxy> {
late final Color color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
@override
Widget build(BuildContext context) {
return ColoredBox(color: color, child: widget.child);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment