你好,我正在尝试创建一个程序,当我单击Container时,它会更改json文件的布尔状态(从true到false或从false到true),但是我无法这样做,因为有人给我以下错误知道我做错了。谢谢您的帮助 !
错误:
编译器消息:
lib / main.dart:95:7:错误:这些成员的非抽象类“ Program”缺少实现:
-State.build
尝试
-提供实施,
-从超类或mixin继承实现,
-将类标记为抽象,或者
-提供“ noSuchMethod”实现。
类计划扩展了状态{ ^^^^^^^^ ../../Documents/flutter/packages/flutter/lib/src/widgets/framework.dart:1413:10:上下文:此处定义了“ State.build”。 小部件build(BuildContext context); ^^^^^ 在2323ms内重新启动应用程序。
class Portao extends StatefulWidget {
@override
Program createState() => Program();
}
/*--------------------------------------------------*/
class Program extends State<Portao> {
List _toDoList = [];
@override
void initState() {
super.initState();
_readData().then((data) {
setState(() {
_toDoList = json.decode(data);
});
});
}
void _addToDo() {
setState(() {
Map<String, dynamic> newToDo = Map();
newToDo['ok'] = true;
_toDoList.add(newToDo);
_saveData();
});
}
Future<Null> _refresh() async{
await Future.delayed(Duration(seconds: 1));
return null;
}
@override
Widget buildItem(BuildContext context, int index) {
return Scaffold(
bottomNavigationBar: CurvedNavigationBar(
index: 0,
height: 75,
items: <Widget>[
Icon(Icons.home, size: 30, color: Colors.black), //0
Icon(Icons.camera_alt, size: 30, color: Colors.black), //1
Icon(Icons.build, size: 30, color: Colors.black), //2
Icon(Icons.person, size: 30, color: Colors.black), //3
],
color: Colors.white.withOpacity(0.90),
buttonBackgroundColor: Colors.white.withOpacity(0.90),
backgroundColor: selected ? Colors.green.withOpacity(0.90) : Colors.deepOrangeAccent.withOpacity(0.90),
animationCurve: Curves.easeInOut,
animationDuration: Duration(
milliseconds: 550
),
onTap: (index) {
if (index==1)
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child: Camera()
)
);
else if (index==2)
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child:Definicoes()),
);
else if (index==3)
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child:infor()),
);
},
),
body: Container(
color: _toDoList[index]['ok'] ? Colors.green.withOpacity(0.90) : Colors.deepOrangeAccent.withOpacity(0.90),
child:Column(
children: <Widget>[
Expanded(
flex: 5,
child: Align(
child: AnimatedContainer(
alignment: _toDoList[index]['ok'] ? Alignment(0, 0.3) : Alignment(0, 0.3),
duration: Duration(
milliseconds: 600),
child: AnimatedContainer(
width: _toDoList[index]['ok'] ? 150 : 135,
height: _toDoList[index]['ok'] ? 150 : 135,
color: Colors.transparent,
duration: Duration(
milliseconds: 1250),
curve: Curves.bounceOut,
child: GestureDetector(
onTap: () {
onChanged: (c){
setState(() {
_toDoList[index]['ok'] = c;
_saveData();
});
};
},
child: Container(
decoration: BoxDecoration(
color: _toDoList[index]['ok'] ? Colors.green.withOpacity(0.90) : Colors.red.withOpacity(0.90),
borderRadius: _toDoList[index]['ok'] ? BorderRadius.circular(30) : BorderRadius.circular(40),
boxShadow: [
BoxShadow(
color: Colors.white.withOpacity(0.40),
spreadRadius: _toDoList[index]['ok'] ? 10 : 4,
blurRadius: 50,
offset: Offset(0, 0),
),
],
),
child: Center(
child: Container(
height: 250,
width: 250,
color: Colors.transparent,
child: _toDoList[index]['ok'] ? FlareActor(
"animacoes/open.flr", animation: 'open',
): FlareActor(
"animacoes/close.flr", animation: 'close',
),
),
),
),
),
),
),
),
),
Expanded(
flex: 5,
child:Row(
children: <Widget>[
Expanded(
flex: 10,
child:Align(
alignment:Alignment(0, -0.3),
child: Container(
width: 250,
height: 250,
child: (
_toDoList[index]['ok'] ? Text(
'Portão Fechado',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 30,
color: Colors.black,
),
textAlign: TextAlign.center,
) : Text(
'Portão aberto',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 30,
color: Colors.black,
),
textAlign: TextAlign.center,
)
),
),
),
),
],
),
),
],
)
),
);
}
Future<File> _getFile() async {
final directory = await getApplicationDocumentsDirectory();
return File("${directory.path}/data.json");
}
Future<File> _saveData() async {
String data = json.encode(_toDoList);
final file = await _getFile();
return file.writeAsString(data);
}
Future<String> _readData() async {
try {
final file = await _getFile();
return file.readAsString();
} catch (e) {
return null;
}
}
}