如何从gridview中选择图像而不重建flutter中的未来构建器



我试图选择图像并在选择图像时给它们一个背景。我的问题是,当我选择一个图像,未来的构建器从数据库重新加载图像,所以我选择的图像不能被看到。我想从数据库中保存数据而不重新加载它。另外,当我使用滑块放大或缩小画廊,未来的建设者重新加载数据,所以它现在给了一个流畅的体验。我希望你能理解我的问题。

return Scaffold(
appBar: AppBar(
title: Text(
"Image",
),
elevation: 0,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.add,
color: Colors.white,
),
onPressed: () => scan(context, selectLocation.id),
)
],
backgroundColor: Colors.transparent,
),
body: FutureBuilder(
future: Provider.of<Images>(context, listen: false).fetchAndSetPlaces(),
builder: (ctx, snapshot) => snapshot.connectionState ==
ConnectionState.waiting
? Center(
child: CircularProgressIndicator(),
)
: Column(
children: [
Expanded(
flex: 2,
child: Container(
padding: EdgeInsets.only(
top: 30, bottom: 1, left: 40, right: 40),
child: Consumer<Images>(
builder: (ctx, titles, ch) => GridView.builder(
physics: ScrollPhysics(),
itemCount: titles.items.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: getSize(_currentSliderValue),
mainAxisSpacing: 50,
childAspectRatio: 0.8,
crossAxisSpacing: 5,
),
itemBuilder: (ctx, index) {
return GestureDetector(
onTap: () => add(titles.items[index].image),
child: Container(
color: selectedCard == index
? Colors.blue
: Colors.green,
child: Image.file(titles.items[index].image),
),
);
},
),
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.only(left: 30, right: 30),
child: Theme(
data: Theme.of(context).copyWith(
accentTextTheme: TextTheme(
bodyText2: TextStyle(color: Colors.white)),
),
child: Slider(
inactiveColor: Colors.purple,
activeColor: Colors.purple,
value: _currentSliderValue,
min: 0,
max: 2,
divisions: 2,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
});
},
),
),
),
),
],
),
),
);

尝试调用你的函数在init状态或在DidChangeDependencies覆盖函数的状态类,而不是使用Future Builder,所以它不会从数据库重新加载图像,因为它只会调用一次。希望你能明白我的意思,否则你可以在下面评论

最新更新