Skip to content

Instantly share code, notes, and snippets.

@MariaMelnik
MariaMelnik / main.dart
Created February 11, 2024 13:25
flutter: suffixIcon alignment
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
@MariaMelnik
MariaMelnik / main.dart
Last active August 18, 2022 10:18
flutter: check bloc updates
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@MariaMelnik
MariaMelnik / main.dart
Created June 17, 2022 09:45
dart: stop future with CancelableOperation
import 'package:async/async.dart';
void main() async {
final cancellableOperation = CancelableOperation.fromFuture(operation());
cancellableOperation.then((p0) => print('cancelable operation finished normally'),
onCancel: () => print('cancelable operation was canceled'));
await Future.delayed(Duration(seconds: 1)).then((_) {
cancellableOperation.cancel();
print('operation cancelled');
});
@MariaMelnik
MariaMelnik / main.dart
Created July 13, 2021 16:51
flutter: widgets vs build methods
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. 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';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
void main() {
final inter = 23.96;
final minVal = 23.6;
final list1 = List.generate(5, (i) => minVal + i * inter);
print(list1);
int i = 0;
double val = minVal;
@MariaMelnik
MariaMelnik / main.dart
Created March 15, 2021 12:23
dart: lazy fibonacci generator
void main() {
final fib = fibStream();
print(fib.take(10));
}
Iterable<int> fibStream() sync* {
final firstIt = fromList([0]).followedBy(fibStream());
final secondIt = fromList([0,1]).followedBy(fibStream());;
yield* zipWith(sumInt, firstIt, secondIt);
}
import 'package:meta/meta.dart';
void runner() {
try {
repository();
} on RepositoryException catch (error, stackTrace) {
// Usually here you not only can log it but show some meaningfull information
// based on [RepositoryException.type]. So you don't even need to know how works // repository inside.
print('error: $error\nStackTrace:$stackTrace');
}
import 'package:flutter/widgets.dart';
import 'new_sb_base.dart';
class MyStreamBuilder<T> extends MyStreamBuilderBase<T, AsyncSnapshot<T>> {
/// Creates a new [MyStreamBuilder] that builds itself based on the latest
/// snapshot of interaction with the specified [stream] and whose build
/// strategy is given by [builder].
///
/// The [initialData] is used to create the initial snapshot.
///
import 'dart:async';
import 'package:flutter/material.dart';
abstract class MyStreamBuilderBase<T, S> extends StatefulWidget {
/// Creates a [MyStreamBuilderBase] connected to the specified [stream].
const MyStreamBuilderBase({ Key key, this.stream }) : super(key: key);
/// The asynchronous computation to which this builder is currently connected,
/// possibly null. When changed, the current summary is updated using
@MariaMelnik
MariaMelnik / main.dart
Created June 30, 2020 18:11
Flutter: StreamBuilder inside StreamBuilder (web test).
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);