Flutter | Load datatable after application load - flutter

I want to load data table after application load. But when application load automatically data table also load. I need that data table load with button click event.
This is the primary widget.
Widget build
Container(
child: OutlineButton(
child: Text('UPDATE',style: TextStyle(color: Colors.white)),
color: Colors.orange,
onPressed: () {abc();},
),
),
itemCount==1 ? Container(
padding: EdgeInsets.all(15.0),
child: SingleChildScrollView(
child: Column(
children: [
activityHistory()
],
),
),
):
I add that Data table to list view builder. I want to call that from button click.
Called Datatable Widget
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
if (itemCount != 0) {
print("OK");
return Container(
child:DataTable(
sortAscending: sort,
sortColumnIndex: 0,
columns: [
DataColumn(
label: Text("Code", style: TextStyle(fontSize: 16)),
numeric: false,
onSort: (columnIndex, ascending) {
setState(() {
sort = !sort;
});
onSortColum(columnIndex, ascending);
}),
DataColumn(
label: Text("QTY", style: TextStyle(fontSize: 16)),
numeric: false,
),
],
rows: avengers
.map(
(avenger) => DataRow(
selected: selectedAvengers.contains(avenger),
cells: [
DataCell(
Text(avenger.name),
onTap: () {
print('Selected ${avenger.name}');
},
),
DataCell(
Text(avenger.weapon),
),
]),
).toList(),
),
);
}else {
print("SizedBox");
return SizedBox();
}
}
);}
Now that Data table load with the application . But i want it load after application load with button click .
How can i load that table after application load and button click event ?

Initially button is NOT clicked - show SizedBox. When button is clicked we pass callback function to onPressed and show ListView.
setState(() {
clicked = true;
});
Something like this.
class _MyHomePageState extends State<MyHomePage> {
bool clicked = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Load list on click"),
),
body: activityHistory(),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
clicked = true;
});
},
child: Icon(Icons.play_arrow),
),
);
}
Widget activityHistory() {
if (!clicked) return SizedBox();
return ListView(
children: [
Text("Item 1"),
Text("Item 2"),
Text("Item 3"),
Text("Item 4"),
Text("Item 5"),
],
);
}
}

Related

I cannot send data to home screen using Flutter/Dart

I did a todo list, I want to send data home screen but I cannot.
I want to get data from page 2 with to do list app and create object on page 1 and add it to the list, but I can't send it with constructor.
class IlkEkran extends StatefulWidget {
String? works;
toDolist("Limon ", DateTime.now())
];
toDolist selectedIndex = toDolist.testerobject();
String? works;
_IlkEkranState({String? works}) { error is here
this.works = works;
}
#override
Widget build(BuildContext context) {
return Container(
child: Column(children: [
Flexible(
child: ListView.builder(
itemCount: listem.length,
itemBuilder: (context, index) {
return Card(
margin: EdgeInsets.all(5),
elevation: 20,
child: ListTile(
title: Text(listem[index].yapilacaklar),
subtitle: Text(listem[index].tarih.toString()),
leading: CircleAvatar(child: Icon(Icons.shopping_basket)),
trailing: Wrap(
spacing: 5,
children: [
IconButton(
onPressed: () {
selectedIndex = listem[index];
setState(() {
listem.remove(selectedIndex);
});
},
icon: Icon(Icons.delete)),
IconButton(
onPressed: () {
setState(() {
toDolist newWork =
toDolist(works!, DateTime.now());
listem.add(newWork);
});
},
icon: Icon(Icons.notification_important)),
],
),
),
);
}),
),
]),
);
}
}

how to hide onStepContinue button for the last step and onStepCancel for the first step on flutter?

This is my first time on flutter framework and Dart programming I have so manny miss of understanding Dart language.
I want to hide onStepContinue button for the last step and onStepCancel for the first step on flutter? this is my code I'm newbe in flutter any help?
class _MyHomePageState extends State<MyHomePage> {
int _currentStep = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Stepper(
controlsBuilder: (BuildContext context, ControlsDetails details) {
return Row(
children: <Widget>[
TextButton(
onPressed: details.onStepContinue,
child: const Text('NEXT'),
),
TextButton(
onPressed: details.onStepCancel,
child: const Text('BACK'),
),
],
);
},
steps: const [
Step(
title: Text("Step 1"),
content: Text("Information for step 1"),
),
Step(
title: Text("Step 2"),
content: Text("Information for step 2"),
),
Step(
title: Text("Step 3"),
content: Text("Information for step 3"),
),
],
onStepTapped: (int newIndex){
setState(() {
_currentStep = newIndex;
});
},
currentStep: _currentStep,
onStepContinue: () {
if (_currentStep != 2) {
setState(() {
_currentStep += 1;
});
}
},
onStepCancel: () {
if (_currentStep != 0) {
setState(() {
_currentStep -= 1;
});
}
},
),
),
);
}
}
Use conditional state to show the buttons.
controlsBuilder: (BuildContext context, ControlsDetails details) {
return Row(
children: <Widget>[
if (_currentStep != 2) // skip on last step
TextButton(
onPressed: details.onStepContinue,
child: const Text('NEXT'),
),
if (_currentStep != 0)// skip on 1st step
TextButton(
onPressed: details.onStepCancel,
child: const Text('BACK'),
),
],
);
},
Wrap your widget using Visibility like this,
bool goneVisibilty=true;
Visibility(
child: Text("Gone"),//replace with your widget
visible: goneVisibility,
),
when ever u need to update this widget call setState to update.
setState((){
goneVisibility=false;
});
To Enable/Disable Button you can use this
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool _isButtonDisabled;
#override
void initState() {
_isButtonDisabled = false;
}
void _incrementCounter() {
setState(() {
_isButtonDisabled = true;
_counter++;
});
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("The App"),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
_buildCounterButton(),
],
),
),
);
}
Widget _buildCounterButton() {
return new RaisedButton(
child: new Text(
_isButtonDisabled ? "Hold on..." : "Increment"
),
onPressed: _isButtonDisabled ? null : _incrementCounter,
);
}
}

Customize Stepper in Flutter

I want to customize the Steppers in flutter.
How can I place the Step title to the left of each step bar?
How can I change the line bar to the dotted bar of the stepper?
And how can I customize the state of the step other than the 5 StepState provided like a round bubble?
Here is my code.
Container(
child: Stepper(
currentStep: 0,
controlsBuilder: (BuildContext context, {
VoidCallback onStepContinue,
VoidCallback onStepCancel
}) => Container(),
steps: [
Step(
content: Container(
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text('9:00Am - 10:00AM'),
Text(
'Everest Tour',
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
),
),
title: Text('9:00 AM'),
),
Step(
content: Text('2nd Data'),
title: Text('10:00 AM'),
state: StepState.editing,
isActive: true,
subtitle: Text('subtitle')
)
],
),
)
You can customize the Stepper widget by modifying the original Stepper widget class. Here's a customized Stepper widget I've created that can be configured with optional stepIndicatorAlignment and dottedLine arguments. The text on 'Continue' and 'Cancel' buttons can also be modified.
Here's a sample on how it can be implemented.
CustomStepper(
// stepIndicatorAlignment is set to StepIndicatorAlignment.left by default if not configured
stepIndicatorAlignment: StepIndicatorAlignment.right,
// dottedLine is set to false by default if not configured
dottedLine: true,
currentStep: _index,
onStepCancel: () {
if (_index > 0) {
setState(() {
_index -= 1;
});
}
},
onStepContinue: () {
if (_index <= 0) {
setState(() {
_index += 1;
});
}
},
onStepTapped: (int index) {
setState(() {
_index = index;
});
},
steps: <CustomStep>[
CustomStep(
title: const Text('Step 1 title'),
content: Container(
alignment: Alignment.centerLeft,
child: const Text('Content for Step 1'),
),
continueButtonLabel: 'YES',
cancelButtonLabel: 'NO',
),
CustomStep(
title: Text('Step 2 title'),
content: Container(
alignment: Alignment.centerLeft,
child: Text('Content for Step 2'),
),
),
],
),
In this similar Stack Overflow post, I've explained how I'm able to modify the stock Flutter Stepper widget.

Build function returned null

I have an app which generates a new Card wrapped in a GestureDetector when the FAB of Scaffold is pressed. the app was working fine but i wanted to implement a delete card functionality and when i added that, the app doesnt recognize the return statements in the build function. I feel like im missing something obvious but since i am new to flutter i am struggling to find what went wrong.
Whole code:
class _Starting_screenState extends State<Starting_screen> {
int _count = 1;
#override
Widget build(BuildContext context) {
{
List<Widget> cardList = new List.generate(
_count, (int i) => new createCard());
SystemChrome.setEnabledSystemUIOverlays([]);
_deleteNoDo(int id, int index) async {
debugPrint("Deleted Item!");
setState(() {
cardList.removeAt(index);
});
void addItems() async {
setState(() {
cardList.insert(0, new GestureDetector(
onTap: () async {
await Navigator.push(context, MaterialPageRoute(
builder: (context) =>
TodoList(), // this just navigates to another screen ; not important in this question
)
);
},
child: Card(
child: ListTile(
title: Text("project 1"),
trailing: new Listener(
key: new Key(UniqueKey().toString()),
child: new Icon(Icons.remove_circle,
color: Colors.redAccent,),
onPointerDown: (pointerEvent) => _deleteNoDo(id, index),
),
subtitle: whitefontstylemont(text: "project 1",
size: 20,)) //this is just a custom TextStyle
),
));
});
}
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () async {
setState(() {
_count += 1;
});
},
heroTag: "btn2",
child: Icon(Icons.add, color: Color(whitecolor),),
backgroundColor: Color(redcolor),),
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
flexibleSpace: FlexibleSpaceBar(
),
actions: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 20, right: 10),
child: whitefontstyle(
text: "Remaining tasks for today - ${cardList
.length}", size: 20,),
),
),
],
),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
delegate: new SliverChildBuilderDelegate((context,
index) {
return cardList[index];
},
childCount: cardList.length
)
),
]
)
);
}
}
}
}
delete function:
_deleteNoDo(int id, int index) async {
debugPrint("Deleted Item!");
setState(() {
cardList.removeAt(index);
});
function which adds a card :
void addItems() async {
setState(() {
cardList.insert(0, new GestureDetector(
onTap: () async {
await Navigator.push(context, MaterialPageRoute(
builder: (context) =>
TodoList(), // this just navigates to another screen ; not important in this question
)
);
},
child: Card(
child: ListTile(
title: Text("project 1"),
trailing: new Listener(
key: new Key(UniqueKey().toString()),
child: new Icon(Icons.remove_circle,
color: Colors.redAccent,),
onPointerDown: (pointerEvent) => _deleteNoDo(id, index),
),
subtitle: whitefontstylemont(text: "project 1", size: 20,)) //this is just a custom TextStyle
),
));
});
}
code where cards are displayed in a list
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
delegate: new SliverChildBuilderDelegate((context, index) {
return cardList[index]; // this is where the cards are displayed in a list
},
childCount: cardList.length
)
)

How do I build objects of a class to use in ListView.builder?

I made a class of cars (has multiple strings such as price and brand.. etc) and I am trying to build a list of it so I can use it when building cards in a ListView but nothing shows up in the screen when I run the code.
I get an error that says vertical viewport was given unbounded height and my app does not show any cards (it does show anything else on the screen though)
My class:-
class cars {
String brand, type, model, color, price, pic;
cars(String b, String t, String m, String c, String p1, String p2) {
brand = b;
type = t;
model = m;
color = c;
price = p1;
pic = p2;
}
}
(page class)
class CHomePage extends State<CHP> {
int i = 0;
String price;
int selected = 0;
List<String> prices = ["Any", "<= 1200", "<= 1600", "<= 1800", "<= 2000"];
List<cars> myCars = new List();
void carsBuilder() {
cars c = new cars(
"Chevorlet", "Corvette Stingray", "2019", "Turqoise", "\$2100",
"assets/Images/corvette.jpg");
myCars.add(c);
c = new cars("Chevorlet", "Impala", "1967", "Black", "\$1900",
"assets/Images/impala.jpg");
myCars.add(c);
c = new cars(
"Dodge", "Challenger SRT Hellcat", "1967", "Dark Red", "\$2000",
"assets/Images/challenger.jpg");
myCars.add(c);
}
Widget buildPicker() {
return CupertinoPicker(
itemExtent: 50,
backgroundColor: CupertinoColors.white,
onSelectedItemChanged: (index) {
setState(() {
selected = index;
});
},
children: List<Widget>.generate(
prices.length,
(index) {
return Center(
child: Text(
prices[index],
style: TextStyle(fontSize: 18, color: Color(0xff469ABF)),
),
);
},
));
}
void incrementTab(index) {
setState(() {
i = index;
if (i == 1) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CAP()),
);
} else if (i == 2) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LP()),
);
}
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: CupertinoNavigationBar(
middle: Text(
'Home Page',
style: TextStyle(color: Color(0xff469ABF)),
),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Padding(
padding: EdgeInsets.all(20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
"Choose Price Range: ",
style: TextStyle(fontSize: 18, color: Color(0xff469ABF)),
),
new CupertinoButton(
onPressed: () async {
await showModalBottomSheet<int>(
context: context,
builder: (BuildContext context) {
return buildPicker();
},
);
},
child: Text(prices[selected]),
),
],
),
),
new ListView.builder(
itemCount: myCars.length,
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
new ListTile(
leading: Icon(Icons.directions_car),
title: Text(myCars[index].type),
subtitle: Text(myCars[index].price),
),
new ButtonTheme.bar(
child: ButtonBar(
children: <Widget>[
new CupertinoButton(
child: Text("View More Details"),
onPressed: () {},
),
],
),
),
],
),
);
},
),
],
),
),
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
primaryColor: Colors.lightBlue,
textTheme: Theme.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.black))),
child: new BottomNavigationBar(
items: [
BottomNavigationBarItem(
title: new Text('Home'),
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
title: new Text('Account'),
icon: Icon(Icons.person),
),
BottomNavigationBarItem(
title: new Text('Logout'),
icon: Icon(Icons.directions_run),
),
],
currentIndex: i,
onTap: (index) {
incrementTab(index);
},
),
),
);
}
}
UPDATE:-
I added those 2 lines to my ListView.builder:-
scrollDirection: Axis.vertical,
shrinkWrap: true,
and put the parent column inside a listview and made the builder a child of it instead of being a child to the column..
My items show and i can scroll ONLY when i press somewhere specific.. other than that it doesnt scroll..
https://gyazo.com/f221fe659df002032ef7b56af5da4a56
where are you initializing the cars list
#override
void initState() {
// TODO: implement initState
super.initState();
carsBuilder();
}