【发布时间】:2021-03-26 01:29:53
【问题描述】:
我想添加一个小吃栏,告诉用户在添加到正文的图像超过 2 时向上滚动。问题是我不能将小吃栏放在脚手架中,因为添加图像按钮只是脚手架小部件中的引用。我刚开始提问,所以我可能没有正确提问。
class App extends StatefulWidget {
createState() {
return AppState();
}
}
class AppState extends State<App> {
int counter = 0;
List<ImageModel> images = [];
void fetchImage() async {
counter++;
while (counter > 2) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Scroll Up'),
duration: Duration(seconds: 2),
));
}
var response =
await get('http://jsonplaceholder.typicode.com/photos/$counter');
var imageModel = ImageModel.fromJson(json.decode(response.body));
setState(
() {
images.add(imageModel);
},
);
}
Widget build(context) {
return MaterialApp(
home: Scaffold(
body: ImageList(images),
backgroundColor: Colors.blueGrey.shade200,
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: Container(
height: 50.0,
//color: Colors.blue,
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.blueGrey,
child: Icon(Icons.add),
onPressed: fetchImage,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
appBar: AppBar(
title: Text("Let's see images!"),
),
),
);
}
}
【问题讨论】:
标签: flutter flutter-layout snackbar