我有这个测试应用程序部分工作。它会更新百吉饼计数器,但会更新屏幕上的所有百吉饼计数器。我将 cubit build.provider 移动到仅在屏幕上显示每个项目计数的文本字段。
这修复了我对所有百吉饼计数的全局更新。所以现在每个人都有自己的计数器。但是现在当我滚动屏幕时,计数都被重置了。我将 + 0 1 计数器作为一个类放入列表视图中。
我试过添加键和 addAutomaticKeepAlives: true 但没有运气。修复删除+之间计数器0的滚动 - 被重置..
主要.dart
//TODO Screen Layouts
//TODO json pull and post
//TODO where to send post to email sms for store to see
//TODO Update live db with fields add to test dp retailp and mobileactive
//TODO Create batch process to generate json file from db nightly or on demain via a button
//TODO TEST code
//TODO spining on load bagel icon or sprit - make.
//TODO total bagels / breads order
//TODO shopping card added
//TODO snackbar menu for as total add up pop up totals
import 'package:flutter/material.dart';
import 'Services.dart';
import 'Product.dart';
import 'bagelcounter.dart';
import 'cubit/counter_state.dart';
import 'cubit/counter_cubit.dart';
import 'cubit/counter_cubit_page.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; //addded
void main() => runApp(MyApp());
// #docregion MyApp
class MyApp extends StatelessWidget {
final bool keepAlive = true; // not working
// #docregion build
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Startup Name Generator',
theme: ThemeData(
// Add the 3 lines from here...
primarySwatch: Colors.blue,
textTheme: TextTheme(
headline1: TextStyle(
fontFamily: 'Corben',
fontWeight: FontWeight.w700,
fontSize: 24,
color: Colors.black,
),
),
), // ... to here.
home: buildJsonParseDemo(),
// org home: buildJsonParseDemo(),
);
}
JsonParseDemo buildJsonParseDemo() => JsonParseDemo();
// #enddocregion build
}
class JsonParseDemo extends StatefulWidget {
//
JsonParseDemo() : super();
@override
_JsonParseDemoState createState() => _JsonParseDemoState();
}
class _JsonParseDemoState extends State<JsonParseDemo> {
//
List<Product> _product;
bool _loading;
//quick fix make this getter
//static //todo make live counter
int mbakerdoz = 0;
//get bakerdoz => mbakerdoz;
//static //todo make live counter
int singles = 10;
@override
void initState() {
super.initState();
_loading = true;
Services.getUsers().then((product) {
setState(() {
_product = product;
_loading = false;
});
});
}
//TODO find day of week to only see items available on day:
//DateTime date = DateTime.now();
//print("weekday is ${date.weekday}");
//TODO Add quantiy field of 1 - 6 , default 0 ,to bagel and breads
//TODO Bottom total of item and price. Tax not included
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_loading ? 'Loading...' : 'Bagels & Breads'),
),
body: Container(
color: Colors.white,
child: ListView.separated(
key: UniqueKey(),
addAutomaticKeepAlives: true, //not working to sotp skroll delete
// key: UniqueKey(), key not fix update all.
separatorBuilder: (context, index) => Divider(
color: Colors.black,
thickness: 2,
),
itemCount: null == _product ? 0 : _product.length,
itemBuilder: (context, index) {
Product product = _product[index];
return ListTile(
key: UniqueKey(),
isThreeLine: true,
// leading: Icon(Icons.plus_one),
// trailing: Icon(Icons.exposure_minus_1),
title: Text(product.pname,
style: TextStyle(
color: Colors.blue[900],
fontSize: 22.0,
fontWeight: FontWeight.w500)),
// product name
subtitle: Text(
'\$ ' +
(product.retailp.toStringAsFixed(2) +
' each index:' +
'$index ' +
' qty ' +
product.qty.toString()),
key: UniqueKey(),
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
fontWeight: FontWeight.w400)
),
trailing: SizedBox(
key: UniqueKey(),
width: 150,
child: BlocProvider<CounterCubit>(
key: UniqueKey(),
create: (context) => CounterCubit(),
child: CounterCubitPage(),
)),
);
},
),
),
bottomNavigationBar: BottomAppBar(
child: Row(
children: [
//IconButton(icon: Icon(Icons.menu), onPressed: () {}),
Spacer(),
Container(
height: 55.0,
width: 1.0,
),
//TODO get bakerdoz and etotal on footer working need to pass data between main and bagelcounter
Text("Baker's Dozen: $mbakerdoz " + " Singles: $singles",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w500)),
Spacer(),
//IconButton(icon: Icon(Icons.search), onPressed: () {}),
//IconButton(icon: Icon(Icons.more_vert), onPressed: () {}),
],
),
shape: CircularNotchedRectangle(),
color: Colors.lightBlue,
notchMargin: 8.0,
),
//floatingActionButton:
// FloatingActionButton(child: Icon(Icons.add), onPressed: () {}),
//floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
}
counter_cubit_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'counter_cubit.dart';
import 'counter_state.dart';
class CounterCubitPage extends StatelessWidget {
// static const String routeName = '/counter_cubit';
//@override
// bool get wantKeepAlive => true; // not work
// final bool keepAlive = true; // try to keep counter on skroll????
@override
Widget build(BuildContext context) => Scaffold(
key: UniqueKey(),
//appBar: AppBar(title: const Text('Counter Cubit Bloc Double CTest')),
body: BlocBuilder<CounterCubit, CounterCubitState>(
key: UniqueKey(),
builder: (context, state) => Column(
children: [
Row(
key: UniqueKey(),
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.add_circle),
color: Colors.blue,
iconSize: 25,
onPressed: () => context.read<CounterCubit>().increment(),
),
Text(
'${state.totalbagels}',
key: GlobalKey(), // not working
style: Theme.of(context).textTheme.headline4,
),
IconButton(
icon: Icon(Icons.remove_circle),
color: Colors.blue,
iconSize: 25,
onPressed: () => context.read<CounterCubit>().decrement(),
),
],
),
],
),
),
);
}
我试过 addAutomaticKeepAlives: true not working 并尝试在字段上使用唯一键......
欢迎任何指点。我是颤振编码的新手..还有很多东西要学..