Я хочу сделать предстоящие задачи, вроде пользовательского интерфейса, с ползунками для запуска и остановки задач.Эти кнопки отображают анимацию ползунка при скольжении по горизонтали.Я также хочу, чтобы по завершении задачи исчезали из списка.
Однако у меня возникла проблема со списком кнопок с состоянием, когда я не добавляю одну в список, но она по-прежнему отображается.вместо этого кнопка, добавленная в список, исчезает.Вот мой код:
import 'dart:core';
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
State createState() => new MyAppState();
}
class MyAppState extends State<MyApp> {
int state = 0;
@override
Widget build(BuildContext context) {
var redButton = new IconButton(
icon: new Icon(Icons.send, color: Colors.red),
onPressed: null
);
var blueButton = new IconButton(
icon: new Icon(Icons.send, color: Colors.blue),
onPressed: null
);
var clicker = new IconButton(
icon: new Icon(Icons.refresh, color: Colors.green),
onPressed: () {
setState(() {
state = state + 1;
});
}
);
List<Widget> list = new List();
list.add(new Text('$state'));
list.add(clicker);
if (state < 1) {
list.add(new _Button(redButton));
} else {
print ('Not showing red button');
}
print ('Showing blue button');
list.add(new _Button(blueButton));
return new MaterialApp(
title: 'Slide animation',
home: new Scaffold(
appBar: new AppBar(
title: new Center(child: new Text('Flutter stateful widget list bug')),
),
body: new Column(
children: list,
),
),
);
}
}
// Includes animation and other stuff - stripped for now
class _Button extends StatefulWidget {
static int counter = 0;
final Widget button;
final int id = counter++;
_SlidableButtonState state;
_Button(this.button);
@override
State createState() {
state = new _SlidableButtonState(button);
print ('${id} -> $state');
return state;
}
}
class _SlidableButtonState extends State<_Button> {
final Widget button;
_SlidableButtonState(this.button);
@override
void dispose() {
super.dispose();
print ('$this: dispose');
}
@override
Widget build(BuildContext context) {
// Some rendering stuff will come here
return button;
}
}
Когда нажата кнопка щелчка, я ожидаю, что красная кнопка исчезнет, а синяя кнопка появится.Тем не менее, красная кнопка все еще показывает, а ее синяя кнопка исчезает.Вот журналы, показывающие, что синяя кнопка была отключена:
Launching lib/examples/list_bug_flutter.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
Built build/app/outputs/apk/debug/app-debug.apk (31.4MB).
Installing build/app/outputs/apk/app.apk...
I/FlutterActivityDelegate(26592): onResume setting current activity to this
I/flutter (26592): Showing blue button
Syncing files to device Android SDK built for x86...
I/flutter (26592): 0 -> _SlidableButtonState#cfe2f(lifecycle state: created, no widget, not mounted)
I/flutter (26592): 1 -> _SlidableButtonState#2d165(lifecycle state: created, no widget, not mounted)
I/flutter (26592): Not showing red button
I/flutter (26592): Showing blue button
I/flutter (26592): _SlidableButtonState#2d165(lifecycle state: defunct): dispose
I/flutter (26592): Not showing red button
I/flutter (26592): Showing blue button