Skip to content

Instantly share code, notes, and snippets.

View sgruhier's full-sized avatar

Sébastien Gruhier sgruhier

  • https://xilinus.com
  • Saint-Paul en Forêt
  • X @sgruhier
View GitHub Profile
@sgruhier
sgruhier / main.dart
Created May 25, 2024 09:31
Built in animation
return Scaffold(
body: Center(
child: AnimatedContainer(
duration: const Duration(seconds: 1),
width: _toggled ? 200 : 100,
height: _toggled ? 200 : 100,
decoration: BoxDecoration(
color: _toggled ? Colors.blue : Colors.red,
borderRadius: BorderRadius.circular(_toggled ? 20 : 0),
),
@sgruhier
sgruhier / main.dart
Created May 23, 2024 14:55
immutable state, correct riverpod example
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// A simple counter class with immutable state
class Counter {
final int value;
Counter(this.value);
// Method to return a new instance with an incremented value
Counter copyWith({int? value}) {
@sgruhier
sgruhier / main.dart
Created May 23, 2024 14:49
mutable state, wrong riverpod example
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// A simple counter class with mutable state
class Counter {
int value;
Counter(this.value);
}
// Creating a StateNotifier for the counter
@sgruhier
sgruhier / main.dart
Created May 18, 2024 15:02
Riverpod - FutureProvider example
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// Define the FutureProvider for fetching data
final dataProvider = FutureProvider<String>((ref) async {
await Future.delayed(Duration(seconds: 2)); // Simulate network delay
return 'Hello, World!';
});
void main() {
@sgruhier
sgruhier / main.dart
Created May 18, 2024 14:51
counter example with riverpod
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// Define the StateProvider for the counter
final counterProvider = StateProvider<int>((ref) => 0);
void main() {
runApp(
ProviderScope(
child: MyApp(),
@sgruhier
sgruhier / main.dart
Last active May 23, 2024 13:26
Theme with riverpod - StateNotifier
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class ThemeNotifier extends StateNotifier<ThemeData> {
ThemeNotifier() : super(ThemeData.light());
bool isLight() {
return state == ThemeData.light();
}
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@sgruhier
sgruhier / main.dart
Created May 6, 2024 10:30
Custom Paint Animation
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Custom Paint Animation'),
),
@sgruhier
sgruhier / main.dart
Last active May 6, 2024 10:09
Flutter physics animation
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
class DraggableSquare extends StatefulWidget {
const DraggableSquare({super.key});
@override
State<DraggableSquare> createState() => _DraggableSquareState();
}
class _DraggableSquareState extends State<DraggableSquare> with SingleTickerProviderStateMixin {
@sgruhier
sgruhier / page_turn.dart
Last active July 5, 2022 13:20 — forked from slightfoot/page_turn.dart
Page Turn Effect - By Simon Lightfoot. Replicating this behaviour. https://www.youtube.com/watch?v=JqvtZwIJMLo
// MIT License
//
// Copyright (c) 2019 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: