我有多个列表,如果没有为它们分配任何内容,则默认情况下它们需要为空。但我收到这个错误:
class Example {
List<String> myFirstList;
List<String> mySecondList;
Example({
this.myFirstList = []; // <-- Error: default value must be constant
this.mySecondList = [];
});
}
Run Code Online (Sandbox Code Playgroud)
但当然,如果我让它保持不变,我以后就无法更改它:
Example({
this.myFirstList = const [];
this.mySecondList = const [];
});
...
Example().myFirstList.add("foo"); // <-- Error: Unsupported operation: add (because it's constant)
Run Code Online (Sandbox Code Playgroud)
我找到了一种这样做的方法,但是我如何对多个列表执行相同的操作:
class Example {
List<String> myFirstList;
List<String> mySecondList;
Example({
List<String> myFirstList;
List<String> mySecondList;
}) : myFirstList = myFirstList ?? []; // <-- How can I do this with multiple lists?
}
Run Code Online (Sandbox Code Playgroud) 当我滚动到列表视图的底部时,底部的项目被重建。当我滚动到顶部时,我的第一个项目被重建。第一个项目是一张带有可选择筹码的卡片,当发生这种情况时,这些筹码会被取消选中。并且“入口”动画也会重播。我怎么能阻止这个?
这是基本代码(它使用 simple_animations 包,我似乎无法用芯片重现问题,但我仍然有动画问题):
import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List _chips = ['Hello', 'World'];
List _selected = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: …Run Code Online (Sandbox Code Playgroud) 我需要 aColumn扩展到受限宽度。因此它会一直扩展,maxWidth但如果没有足够的空间,也不介意变小。但Expanded忽略了ConstrainedBox.
Row(
children: [
const Spacer(),
Expanded(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 50),
child: Column(
children: [
Expanded(child: Container(color: Colors.red)),
Expanded(child: Container(color: Colors.green)),
],
),
),
),
],
)
Run Code Online (Sandbox Code Playgroud)
这是我的完整上下文代码:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GetBuilder<TimerController>(
builder: (_) => CustomBackground(
color: _timerController.currentChild?.color ?? Colors.blue,
child: SafeArea(
child: Center(
child: Padding(
padding:
EdgeInsets.symmetric(horizontal: AppSizes.pagePadding),
child: OrientationBuilder(
builder: (context, orientation) =>
(orientation == Orientation.portrait)
? ConstrainedBox(
constraints: BoxConstraints( …Run Code Online (Sandbox Code Playgroud) 我需要从父小部件开始一个子小部件的动画。我怎样才能做到这一点?
我试过给父母控制器,但你如何替换vsync: this?
这是基本代码(我还没有真正测试过这段代码,但我展示了我的意思):
import 'package:flutter/material.dart';
class ParentWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
ChildText(),
FlatButton(
child: Text('start the animation'),
onPressed: () {
// start the animation!!!????????
},
)
],
);
}
}
class ChildText extends StatefulWidget {
@override
_ChildTextState createState() => _ChildTextState();
}
class _ChildTextState extends State<ChildText> with TickerProviderStateMixin {
AnimationController _controller;
Animation _animation;
@override
void initState() {
super.initState();
// actual animation is much more complex, this is just a random …Run Code Online (Sandbox Code Playgroud) 我正在开发我的第一个 Flutter ios 应用程序,并创建了一个免费的 Apple 开发者帐户。为了使用 Firebase 动态链接,您需要添加您的 App Store ID 和团队 ID。我在哪里可以找到这些?我需要注册 Apple 开发者计划吗?
有没有办法在 AnimatedList 中设置动画的持续时间?
AnimatedList(
key: _animList,
initialItemCount: _myList.length,
itemBuilder: (context, index, animation) {
// duration = Duration(seconds: 1); <--- ????????
return SlideTransition(
position: animation.drive(
Tween(begin: Offset(1, 0), end: Offset(0, 0))
.chain(CurveTween(curve: Curves.bounceIn))),
child: Container(color: Colors.red, height: 100, width: 100));
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的通知播放我自己的自定义声音。但是在我的 android 模拟器上,它只播放默认声音,而在我自己的设备上,它甚至不发出声音。我试过这个,但它没有帮助我。
FlutterLocalNotificationsPlugin _notifications = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
await _notifications.initialize(initializationSettings);
var androidPlatformChannelSpecifics = AndroidNotificationDetails('test_channel', 'test', '', playSound: true, sound: RawResourceAndroidNotificationSound('my_sound'));
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _notifications.show(0, 'test', 'this is a test', platformChannelSpecifics);
Run Code Online (Sandbox Code Playgroud)
我没有收到任何错误。我的文件在 android/app/src/main/res/raw/my_sound.mp3
有没有办法让 Flutter Web 应用程序进入全屏模式(隐藏地址栏、标签栏和任务栏)?或者有没有办法以编程方式按下 F11?
我试过了...
@override
void dispose() {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}
@override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}
Run Code Online (Sandbox Code Playgroud)
但它在网络应用程序中不起作用(我没想到会这样)
我一直在关注这个使用 BLoC 的NoSQL 教程。但是当我将“fruit_event.dart”中的代码粘贴到 Visual Studio Code 中时,它给了我一个错误。
Fruit_event.dart:
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:sembast_prep/data/fruit.dart';
@immutable
abstract class FruitEvent extends Equatable {
FruitEvent([List props = const []]) : super(props); // error here!
}
class LoadFruits extends FruitEvent {}
class AddRandomFruit extends FruitEvent {}
class UpdateWithRandomFruit extends FruitEvent {
final Fruit updatedFruit;
UpdateWithRandomFruit(this.updatedFruit) : super([updatedFruit]);
}
class DeleteFruit extends FruitEvent {
final Fruit fruit;
DeleteFruit(this.fruit) : super([fruit]);
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误(Visual Studio 代码):
[List<dynamic> props = const []]
Too …Run Code Online (Sandbox Code Playgroud) 我的 Flutter 桌面 Web 应用程序具有复杂的用户界面,很难做出响应。因此,我想将其放在一个FittedBox如果用户缩小浏览器窗口时会简单地缩小整个应用程序的应用程序中。
class CustomPage extends StatelessWidget {
final Widget child;
const CustomPage({
Key? key,
required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: GetPlatform.isDesktop
? FittedBox(
child: SizedBox(
width: Get.width,
height: Get.height,
child: Center(child: child),
),
)
: Text('Sorry! This was only meant for desktop.'),
);
}
}
Run Code Online (Sandbox Code Playgroud)
但是Get.width并且WidgetsBinding.instance.window.physicalSize.width只能得到初始窗口大小。因此,如果应用程序在全屏浏览器中启动,则效果很好,但如果应用程序在小浏览器中启动,则不起作用。MediaQuery.of(context).size.width只获取当前屏幕尺寸。
有没有办法获取桌面物理屏幕尺寸?
如何在 dart 中创建嵌套列表的副本?在此代码中,我对副本所做的更改也会在原始代码中更改
List board = [[0,0,0], [0,0,0], [0,0,0]];
List boardCopy = List.from(board); // create copy of the board
boardCopy[0][0] = 1; // change copy
print(board); // print original board
OUTPUT:
[[1,0,0], [0,0,0], [0,0,0]] <-- it has changed the original board!!!
Run Code Online (Sandbox Code Playgroud) 在 python 中,你可以在方法中运行一个简单的函数,__init__如下所示:
class AuthError:
def __init__(self, message):
self.message = message
print(self.message)
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 Dart 中做到这一点?(我对 dart 和 flutter 还很陌生)
class AuthError {
final String message;
MyExample(this.message); // <-- how do I e.g. print message when AuthError is initialized?
}
Run Code Online (Sandbox Code Playgroud) 如何对包含对象的列表进行排序,以便对象的属性与 dart 中的不同列表相匹配?
class Example {
String _id;
String get id => _id;
}
List examples = [Example(id: 'hey'), Example(id: 'foo'), Example(id: 'baa')]
List ids = ['foo', 'baa', 'hey']
print(examples.sortBy(ids)) ???????
OUTPUT:
[Example(id: 'foo'), Example(id: 'baa'), Example(id: 'hey')]
Run Code Online (Sandbox Code Playgroud) flutter ×12
dart ×7
list ×3
flutter-web ×2
animation ×1
app-store ×1
bloc ×1
deep-copy ×1
default ×1
firebase ×1
flutter-getx ×1
ios ×1
nested-lists ×1
nosql ×1
parameters ×1
sorting ×1