Question: Hello, This is for flutter development. I am learning dart language and can really use some help figuring out how to do the following: Modify
Hello, This is for flutter development. I am learning dart language and can really use some help figuring out how to do the following:
Modify the code to include a Decrement button and randomly generated word pair.
When the Increment button is clicked:
- the counter is incremented,
- a random word pair is generated and displayed, and this word pair is added to the list.
Modify the Decrement buttons code to do the following:
- The counter is decremented only if the counter value is greater than zero.
- The last item in the list is removed, and the changed list is displayed on the device screen.
Below is the given default code for this assignment. I believe It gives a list from an array and then each time you press the increment button it increases a counter by 1 and adds "count = #" to the list. Please modify the code for the assignment. Thank you very much!
Ps: we have to add: "import 'package:english_words/english_words.dart'; " and add "english_words ^3.1.0" underneath "cupertino_icons: ^0.1.2" in the pubspec.yaml file inorder touse the random word generator so says the professor.
////////////////////////////////////////////////////////////////////////////////////////////////////
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
String appTitle = 'Static List' ;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: appTitle,
home: HomePage()
);
}
} // End of MyApp
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar( title: Text(appTitle) ),
body: DisplayCounterAndList(),
);
}
} // end of HomePage
class DisplayCounterAndList extends StatefulWidget{
// The createState method is used to create
// instances of mutable widgets
@override
MyCounterAndListState createState() =>
MyCounterAndListState();
} // End of DisplayCounterAndList
class MyCounterAndListState extends
State
List
'itm 3','itm 4','itm 5'];
int count = 0;
@override
Widget build(BuildContext context) {
return Column(
children:
// column widgets
Row (
children:
RaisedButton(
onPressed: incrementCounter,
child: Text('Increment'),
),
Text(' Counter = $count '),
], // end of row widgets
), // Row
/* Note:
* ListView.builder(...) will not work under
* Column (or Row) unless its wrapped inside
*
*
* the Expanded widget.
* This widget allows Column (or Row) elements
* to expand and fill the available space,
* which is needed by ListView.builder(...)
* */
Expanded (
child: ListView.builder(
itemCount: myList.length,
itemBuilder: buildListBody,
), // ListView.builder
), // Expanded
], // end of widgets_one - column widgets
); // Column
}
void incrementCounter( ){
setState( ( ) {
count ++;
myList.add( 'Count = ' + count.toString() );
} ) ; // setState
} // incrementCounter
// A Function (or Method) that is
// called from itemBuilder
Widget buildListBody(BuildContext ctxt, int curIndx) {
return ListTile(
title: Text ( myList[curIndx] ),
);
}
} // end of MyCounterAndListState
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
