flutter一些使用方式

1、本地文件和图片调用方式
在pubspec.yaml加入assets路径如以下代码,在flutter中即可通过Image.asset('assets/images/pic.jpg') 引入该路径下的图片。

  assets:
    - assets/
    - assets/images/

2、flex布局占位

Row(
        children: [
          Expanded(
            child: Container(
              color: Colors.red,
              padding: EdgeInsets.all(5.0),
            ),
            flex: 1,
          ),
          Expanded(
            child: Container(
              color: Colors.yellow,
              padding: EdgeInsets.all(5.0),
            ),
            flex: 2,
          ),
        ],
),

在底部导行栏小图标上加数值

BottomNavigationBarItem(
    icon: Stack(
        children: [
            Icon(Icons.contacts),
            Positioned(
                right: 0,
                child: Container(
                    padding: EdgeInsets.all(1),
                    decoration: BoxDecoration(
                        color: Colors.red,
                        borderRadius: BorderRadius.circular(6),
                    ),
                    constraints: BoxConstraints(
                        minWidth: 13,
                        minHeight: 10,
                    ),
                    child: Text(
                        '122',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 8,
                        ),
                        textAlign: TextAlign.center,
                    ),
                ),
            )
        ],
    ),
    title: Text('联系人')
),

image.png

播放本地mp3
下载audioplayers库:https://pub.dartlang.org/packages/audioplayers

import 'package:audioplayers/audio_cache.dart';
...
  playLocal() async {
    AudioCache player = new AudioCache();
    const alarmAudioPath = "audio/ruguo.mp3";
    player.play(alarmAudioPath);
  }
...

websocket单例

import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class SingletonWebSocket {
  static final SingletonWebSocket _singleton = new SingletonWebSocket._internal();

  factory SingletonWebSocket() {
    return _singleton;
  }

  SingletonWebSocket._internal();

  final WebSocketChannel channel = IOWebSocketChannel.connect('ws://192.168.3.4/echo');

}

SnackBar不能直接在Scaffold里显示
1)通过widget拆分

  1. 通过GlobalKey
final GlobalKey _scaffoldKey = new GlobalKey();
...
_scaffoldKey.currentState.showSnackBar(
    content: Text('Assign a GlobalKey to the Scaffold'),
    duration: Duration(seconds: 3),
);

你可能感兴趣的:(flutter一些使用方式)