2022/6/20/Dart/Enhanced enumsを使ってみよう

 

Dart2.17で導入されたEnhanced enumsの使い方をサンプルコードで確認しましょう。

まずこれまでのenumでは、それぞれの列挙型の値がint型・String型などのデータを保持しているような実装をしたい場合(下記サンプルではint型)、下記のように書く必要がありました。

enum OldVehicle{
  car,
  bus,
  bicycle,
}

extension OldVehicleX on OldVehicle{

  int get tires{
    switch(this){
      case OldVehicle.car:
        return 4;
      case OldVehicle.bus:
        return 6;
      case OldVehicle.bicycle:
        return 2;
    }
  }

  int get passengers{
    switch(this){
      case OldVehicle.car:
        return 5;
      

2022/6/11/Flutter/FocusableActionDetector

 

FocusableActionDetectorの訳

Material buttons are great but we know they’re not always right for your app so you write your own.

マテリアルボタンは素晴らしいものですが、必ずしもあなたのアプリに適しているとは限らないので、自分で書くことになります。

 

Sadly writing your own controls from scratch can be a lot of work.

悲しいかな、自分で一から制御を書くのは大変な作業です。

 

Desktop users …

2022/6/10/Flutter/MaterialStateProperty

 

結論

スタート時点。TextButtonのbackgroundColorをColors.redで指定しようとするとエラーが出る。

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        //backgroundColor: Colors.red,
        backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states){
          if(true){
            return Colors.cyanAccent;
          }
          return Colors.red;
        }),
      

2022/6/10/Flutter/Type Promotion

 

Type Promotionの訳

If you’ve worked with Dart and Flutter long enough, you’ve probably come across Type Promotion.
DartやFlutterを長く使っていると、Type Promotionに出会ったことがあるのではないでしょうか。

 

It’s a Dart language feature that in certain circumstances allows you to treat variables as if they had a …

2022/6/9/Flutter/ThemeExtension

 

ThemeExtension | Decoding Flutterの訳

When I started my software career as a web developer, one of the first things I learned  was not to mix my semantic information with my presentation rules.

私がweb developerとしてソフトウェアキャリアをスタートした時、初めに学んだことの一つとして、意味的な情報(HTML)とプレゼンテーションルール(CSS)を混合させないことでした。

 

On the web, this …

2022/6/8/Flutter/FractionallySizedBox

 

FractionallySizedBox

 

Sometimes your design calls for dimensions that are relative.

時に、デザインの際、相対的な寸法(サイズ)が必要な場合があります。

 

For example, a button should take 70% of the app’s width, or a margin should take 10% of a widget.

例えば、ボタンはアプリの幅の70%を、ウィジェットは余白(margin)を10%取るのが望ましいとされています。

 

FractionallySizedBox can do just …

2022/5/31/Dart/EffectiveDart/DesignPart2

 

<<パート1へ

Libraries

A leading underscore character ( _ ) indicates that a member is private to its library. This is not mere convention, but is built into the language itself.

先頭のアンダースコア文字( _ )は、そのメンバーがそのライブラリに対してプライベートであることを示します。これは単なる慣習ではなく、言語自体に組み込まれているものです。

ここでのライブラリプライベート=ファイルプライベート


PREFER making declarations private.

宣言はプライベートにしましょう。…