Auto landscape when the user click the full screen video using flutter - flutter

i hope the title is enough to understand my problem is, story line: When the user click full screen. the video automatic landscape. How? please help me
Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
margin: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent)),
child: InAppWebView(
initialUrl:
"https://ip-address/play.html?name=123456789",
initialHeaders: {},
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart:
(InAppWebViewController controller, String url) {
setState(() {
this.url = url;
});
},
onLoadStop: (InAppWebViewController controller,
String url) async {
setState(() {
this.url = url;
});
},
onProgressChanged:
(InAppWebViewController controller, int progress) {
setState(() {
this.progress = progress / 100;
});
},
),
),
),
])),

dependencies: auto_orientation: ^2.0.0
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:auto_orientation/auto_orientation.dart';
void main() {
runApp(
AutoOrientationDemo(),
);
}
class AutoOrientationDemo extends StatefulWidget {
AutoOrientationDemo({this.title = 'Auto Orientation Demo'});
final String title;
#override
State<StatefulWidget> createState() {
return _AutoOrientationDemoState();
}
}
class _AutoOrientationDemoState extends State<AutoOrientationDemo> {
TargetPlatform? _platform;
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: widget.title,
theme: ThemeData.light().copyWith(
platform: _platform ?? Theme.of(context).platform,
),
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.portraitDownMode();
},
child: Padding(
child: Text("Portrait UPSIDE Down"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.fullAutoMode();
},
child: Padding(
child: Text("All modes"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.landscapeAutoMode();
},
child: Padding(
child: Text("Landscape auto"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.portraitAutoMode();
},
child: Padding(
child: Text("Portrait auto"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.landscapeLeftMode();
},
child: Padding(
child: Text("Landscape left mode"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
AutoOrientation.landscapeRightMode();
},
child: Padding(
child: Text("Landscape right mode"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.portraitUpMode();
},
child: Padding(
child: Text("Portrait up mode"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
AutoOrientation.portraitDownMode();
},
child: Padding(
child: Text("Portrait down mode"),
padding: EdgeInsets.symmetric(vertical: 16.0),
),
),
),
],
)
],
),
),
);
}
}
This Will Help You For Auto Screen Orientation based on your content

You can use JavaScript channel to get notifications from the web view. See a section on channels here:
https://medium.com/flutter-community/inappwebview-the-real-power-of-webviews-in-flutter-c6d52374209d
Then in your HTML/JavaScript, listen to full the screen event on your video object and send it to the JavaScript channel to be handled in Flutter app.

Related

How to stack two bottom sheet in flutter?

I want to stack two bottom sheet each other in flutter as show in photo. The upper one is shown when in error state. In photo, it build with alert dialog. I want is with bottom sheet. How can I get it?
Edit:
Here is my code that I want to do. Lower bottom sheet is with pin field, autoComplete. autoComplete trigger StreamController, and then streamBuilder watch Error state and show dialog.
confirmPasswordModalBottomSheet(
BiometricAuthRegisterBloc biometricAuthRegBloc) {
showMaterialModalBottomSheet(
context: context,
builder: (BuildContext context) {
return StreamBuilder(
stream: biometricAuthRegBloc.biometricAuthRegisterStream,
builder: (context,AsyncSnapshot<ResponseObject>biometricAuthRegSnapShot) {
if (biometricAuthRegSnapShot.hasData) {
if (biometricAuthRegSnapShot.data!.messageState ==
MessageState.requestError) {
showModalBottomSheet(context: context, builder:
(BuildContext context){
return Container(
width: 200,height: 200,
child: Center(child: Text('Helllllllllo'),),);
});
}
}
return SizedBox(
width: 100,
height: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: margin30,
),
Text(CURRENT_PIN_TITLE),
SizedBox(
height: margin30,
),
Padding(
padding: const EdgeInsets.only(
left: margin60, right: margin60),
child: PinCodeField(
pinLength: 6,
onChange: () {},
onComplete: (value) {
biometricAuthRegBloc.biometricAuthRegister(
biometricType:_biometricAuthTypeForApi,
password: value);
},
),
),
SizedBox(
height: margin30,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal:
margin80),
child: AppButton(
onClick: () {},
label: CANCEL_BTN_LABEL,
),
),
Container(
padding: const EdgeInsets.all(8.0),
margin:
EdgeInsets.symmetric(vertical: 8.0,
horizontal: 30),
decoration: BoxDecoration(
color: Colors.grey,
border: Border.all(color: Colors.black),
),
child: const Text(
FINGER_PRINT_DIALOG,
textAlign: TextAlign.center,
),
)
],
),
);
});
},
);
}
When I do like that above, I get setState() or markNeedsBuild() called during build. Error and why? Sorry for my previous incomplete question.
I am bit confused with your question but stacking two bottomsheet is just easy. You just need to call the showModalBottomSheet whenever you want it shown to user. You can check out the following implementation:
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ElevatedButton(
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 500,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet 1'),
ElevatedButton(
child: const Text('Show second modal 2'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.redAccent,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet 2'),
ElevatedButton(
child: const Text('Close BottomSheet'),
onPressed: () => Navigator.pop(context),
),
],
),
),
);
},
);
},
),
],
),
),
);
},
);
},
child: Text('Show bottom sheet 1'),
),
);
}
}
I have solution. All I need to do is, need to add WidgetBinding.insatance.addPostFrameCallback((timeStamp){showModalBottomSheet()}); in the StreamBuilder return.

Flutter: Close DropdownButton (DropdownMenu)

Is there a way to close the selection menu of a DropdownButton containing all the DropdownMenuItems when an onTap function is executed (GestureDetector inside a DropdownMenuItem)?
Here is my implementation of the approach of Alperen Baskaya (in a slightly reduced version so that it is understandable). This approach however does not work yet and I am not sure whether it is because I have implemented it incorrectly or because the approach does not work for my problem.
class _BoatSelectionState extends State<BoatSelection> {
FocusNode focusNode;
#override
void initState() {
super.initState();
focusNode = FocusNode();
}
#override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child:
DropdownButtonHideUnderline(
child: DropdownButton<Boat>(
focusNode: focusNode,
icon: Icon(
Icons.keyboard_arrow_down_rounded,
color: Colors.black,
),
isExpanded: true,
value: selectedBoat,
onChanged: (Boat _boat) => Provider.of<BoatStreamsCubit>(context, listen: false).setBoat(_boat),
selectedItemBuilder: (BuildContext context) {
return widget.boats.map<Widget>((Boat boat) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
BoatClassLogo(boat: boat),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: DesignValues.paddingMd),
child: BoatInformation(boat: boat),
),
),
],
);
}).toList();
},
items: widget.boats.map<DropdownMenuItem<Boat>>((Boat _boat) {
return DropdownMenuItem<Boat>(
value: _boat,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(right: DesignValues.paddingMd),
child: BoatClassLogo(boat: _boat),
),
Expanded(
child: BoatInformation(boat: _boat),
),
GestureDetector(
onTap: () {
focusNode.unfocus();
Navigator.push(context, MaterialPageRoute(builder: (context) => BoatForm(CreationState.edit, _boat)));
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: Icon(
Icons.edit,
color: AppColors.primary,
),
),
),
],
),
);
}).toList(),
),
),
),
],
);
}
}
I looked up the internal implementation of DropdownMenu in dart.
The popover for DropdownMenu is created by using Navigator.push(). It waits for the user to click an item and returns the value with Navigator.pop(). So we can pop the popover manually by getting the dropdown's context via a GlobalKey.
late GlobalKey dropdownKey;
#override
void initState() {
super.initState();
dropdownKey = GlobalKey();
}
...
DropdownButton<Boat>(
key: dropdownKey,
...)
And remove it using Navigator.pop()
GestureDetector(
onTap: () {
Navigator.pop(dropdownKey.currentContext);
Full code:
class _BoatSelectionState extends State<BoatSelection> {
GlobalKey dropdownKey;
#override
void initState() {
super.initState();
dropdownKey = GlobalKey(); // Init GlobalKey, allows to close the DropdownButton
}
#override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child:
DropdownButtonHideUnderline(
child: DropdownButton<Boat>(
key: dropdownKey,
icon: Icon(
Icons.keyboard_arrow_down_rounded,
color: Colors.black,
),
isExpanded: true,
value: selectedBoat,
onChanged: (Boat _boat) => Provider.of<BoatStreamsCubit>(context, listen: false).setBoat(_boat),
selectedItemBuilder: (BuildContext context) {
return widget.boats.map<Widget>((Boat boat) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
BoatClassLogo(boat: boat),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: DesignValues.paddingMd),
child: BoatInformation(boat: boat),
),
),
],
);
}).toList();
},
items: widget.boats.map<DropdownMenuItem<Boat>>((Boat _boat) {
return DropdownMenuItem<Boat>(
value: _boat,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(right: DesignValues.paddingMd),
child: BoatClassLogo(boat: _boat),
),
Expanded(
child: BoatInformation(boat: _boat),
),
GestureDetector(
onTap: () {
Navigator.pop(dropdownKey.currentContext); // Closes the dropdown
Navigator.push(context, MaterialPageRoute(builder: (context) => BoatForm(CreationState.edit, _boat)));
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: Icon(
Icons.edit,
color: AppColors.primary,
),
),
),
],
),
);
}).toList(),
),
),
),
],
);
}
}
If I got you right you can use focus node for dropdown menu.
FocusNode dropdown;
Initializing in initstate is needed;
dropdown = FocusNode();
child: DropdownButtonHideUnderline(
child: DropdownButton <String>(
focusNode: dropdown,
Then when you may think to close this menu execute in ontap;
dropdown.unfocus();

Future builder not updating once i have resposne

I have a page where once i have response i need to display items
here is my code
It keeps on showing loader and futureBuilder never gets triggered
#override
void initState() {
super.initState();
helper.getString(TOKEN_ID).then((value) {
userResponseFuture = repos.getUserDetails(value);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.white,
child: FutureBuilder<UserResponse>(
future: userResponseFuture,
builder: (context, snapShot) {
if (snapShot.hasData) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
CircleAvatar(
radius: 50.0,
backgroundImage: NetworkImage(snapShot
.data.profilePictureURL ??
"https://cdn.mos.cms.futurecdn.net/5e7ehPZf5RT6Jt2H9cQP6k-1024-80.jpg.webp"),
backgroundColor: Colors.brown,
),
FlatButton(
child: Row(
children: [
Icon(Icons.edit),
Text("Edit profile Image"),
],
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ProfilePicScreen()));
},
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
snapShot.data.firstName +
" " +
snapShot.data.lastName,
style: TextStyle(color: Colors.blue.shade900)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(snapShot.data.email,
style: TextStyle(color: Colors.blue.shade900)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(snapShot.data.phoneNo ?? "",
style: TextStyle(color: Colors.blue.shade900)),
)
],
);
} else {
return SizedBox(
height: 30.0,
width: 30.0,
child: CircularProgressIndicator(
strokeWidth: 2.0,
));
}
},
)),
);
}
}
You need to add setState after assign a value to userResponseFuture;
helper.getString(TOKEN_ID).then((value) {
userResponseFuture = repos.getUserDetails(value);
setState((){})
});
because it is being after build method and your widget can't call the right future function.

Invisibility , Gone , visibility ROW & Column in Flutter

I use this code in Flutter and i want to Visible/Invisible some Row or column .
In android studio and java we use :
msg.setVisibility(View.INVISIBLE);
but how can use Id for Row and widget in Flutter and invisible/visible widget and Row ?
this is my code :
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home : MyHomePage()
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(
body: Column(children: <Widget>[
Row(
//ROW 1
children: [
Container(
color: Colors.lightGreen,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
Container(
color: Colors.orange,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
],
),
Row(
//ROW 1
children: [
Container(
color: Colors.blueAccent,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
Container(
color: Colors.green,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
],
),
]),
bottomNavigationBar: new Container(
color: Colors.redAccent,
height: 55.0,
alignment: Alignment.center,
child: new BottomAppBar(
color: Colors.blueAccent,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new IconButton(icon: new Icon(Icons.add , color: Colors.black), onPressed: (){ print("helllo"); } ),
new IconButton(icon: new Icon(Icons.remove , color: Colors.black), onPressed: (){ print("helllo"); } ),
],
),
)
),
);
}
}//MyHomePage
I want to use IconButton to visible/invisible two Rows.
how i can?
You could use Visibility like this:
Visibility(
visible: true,
child: Text("Visible"),
),
Visibility(
visible: false,
maintainState: true,
maintainAnimation: true,
maintainSize: true,
child: Text("Invisible"),
),
Visibility(
visible: true,
child: Text("Visible"),
),
Visibility(
visible: false,
child: Text("Gone"),
),
Visibility(
visible: true,
child: Text("Visible"),
),
And this would be the result:
Visible
Visible
Visible
Visible
Android (Kotlin)
linear_layout.visibility = View.VISIBLE
Android (AndroidX)
linear_layout.isVisible = true
or
linear_layout.isInvisible = false
or
linear_layout.isGone = false
Flutter
Row(
children: [
Text(
"Stack Overflow",
),
],
);
or
Visibility(
child: Row(
children: [
Text(
"Stack Overflow",
),
],
),
);
Invisible (not visible but maintain space)
Android (Kotlin)
linear_layout.visibility = View.INVISIBLE
Android (AndroidX)
linear_layout.isInvisible = true
Flutter
Visibility(
maintainSize: true,
visible: false,
child: Row(
children: [
Text(
"Stack Overflow",
),
],
),
);
or (when you know the size)
Container(
height: 300,
child: Row(
children: [
Text(
"Stack Overflow",
),
],
),
);
Gone
Android (Kotlin)
linear_layout.visibility = View.GONE
Android (AndroidX)
linear_layout.isGone = true
or
linear_layout.isVisible = false
Flutter
Visibility(
visible: false,
child: Row(
children: [
Text(
"Stack Overflow",
),
],
),
);
There is a special widget called Visibility. Keep in mind the inversion of state management which is used in Flutter. You invoke setState() and condition for visibility of the widget.
And don't forget to change your Widget to StatefulWidget
Refer to
https://api.flutter.dev/flutter/widgets/Visibility-class.html
Usage:
child: Visibility(
visible: false,
),
Here is the sample which should work in your scenario, it hides the rows on Remove button clicked and shows on add:
class MyHomePage extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _WidgetState();
}
}
class _WidgetState extends State<MyHomePage> {
bool visible = true;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: <Widget>[
Visibility(
visible: visible,
child: Row(
//ROW 1
children: [
Container(
color: Colors.lightGreen,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
Container(
color: Colors.orange,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
],
),
),
Visibility(
visible: visible,
child: Row(
//ROW 1
children: [
Container(
color: Colors.blueAccent,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
Container(
color: Colors.green,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
],
),
),
]),
bottomNavigationBar: new Container(
color: Colors.redAccent,
height: 55.0,
alignment: Alignment.center,
child: new BottomAppBar(
color: Colors.blueAccent,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new IconButton(
icon: new Icon(Icons.add, color: Colors.black),
onPressed: () {
print("show");
setState(() {
visible = true;
});
}),
new IconButton(
icon: new Icon(Icons.remove, color: Colors.black),
onPressed: () {
print("hide");
setState(() {
visible = false;
});
}),
],
),
)),
);
}
}
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home : MyHomePage()
);
}
}
class MyHomePage extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _WidgetState();
}
}
class _WidgetState extends State<MyHomePage> {
bool visible = true;
bool visible1 = true;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: <Widget>[
Visibility(
visible: visible1,
child: Row(
//ROW 1
children: [
Container(
color: Colors.orange,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
Container(
color: Colors.orange,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
],
),
),
Visibility(
visible: visible,
child: Row(
//ROW 1
children: [
Container(
color: Colors.green,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
Container(
color: Colors.green,
margin: EdgeInsets.all(25.0),
child: FlutterLogo(
size: 60.0,
),
),
],
),
),
]),
bottomNavigationBar: new Container(
color: Colors.black,
height: 55.0,
alignment: Alignment.center,
child: new BottomAppBar(
color: Colors.blueAccent,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new IconButton(
icon: new Icon(Icons.add, color: Colors.black),
onPressed: () {
print("show");
setState(() {
visible1 = true;
});
}),
new IconButton(
icon: new Icon(Icons.remove, color: Colors.black),
onPressed: () {
print("hide");
setState(() {
visible1 = false;
});
}),
],
),
)),
);
}
}
You can wrap your widget with Visibility Widget like this and pass a flag true and false like this.
Visibility(
visible: false,
child: Row(), //pass your own widget here

I want to delete selected data from the list using the delete button in action bar

I'm new to flutter and I want to delete the selected values from the
list,but I don't know how to delete selected Items,can anyone help?
I have taken icon button in Appbar and I tried to setState in it by
using the .removelast() command,but I want to select the Item then
delete it.
Code :
class DemoPage extends State<MyHomePage> {
TextEditingController Controller = TextEditingController();
List<String> msg = List();
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Demo_App'),
actions: <Widget>[
IconButton(icon: Icon(Icons.delete),
onPressed: (){
setState(() {
msg.removeLast();
});
}),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
alignment: Alignment.topLeft,
margin: EdgeInsets.only(right: 150.0,top: 10.0,left: 8.0),
child:TextField(
controller: Controller,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'please enter your name',
),
),
),
Container(
alignment: Alignment.topRight,
margin: EdgeInsets.only(left: 250.0,right: 10.0),
child: RaisedButton(
onPressed: () {
setState(() {
msg.add(Controller.text);
Controller.clear();
});
},
child: Text('Add'),
),
),
Expanded(
flex: 2,
child: Container(
child: Card(
margin: EdgeInsets.all(8.0),
child: ListView.builder(
itemCount: msg.length,
itemBuilder: (context, index){
if(index.isInfinite){
return Divider();
}
return ListTile(
title: Text(msg[index]),
);
},),
),
)),
],
),
);
}
}
I want to select the data and then delete it using the icon Button in
the AppBar.
Lets assume you want to select your items by a single click.
Take a separate a list indexList and each time you select an item, you store the clicked index into indexList.
Then upon clicking delete button run a loop on indexList and remove items from your itemList using the stored indexes.
clean indexList
update your state
class DemoPage extends State<MyHomePage> {
TextEditingController Controller = TextEditingController();
List<String> msg = List();
List<int> selectedItems = List();
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Demo_App'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
msg.removeLast();
});
}),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
alignment: Alignment.topLeft,
margin: EdgeInsets.only(right: 150.0, top: 10.0, left: 8.0),
child: TextField(
controller: Controller,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'please enter your name',
),
),
),
Container(
alignment: Alignment.topRight,
margin: EdgeInsets.only(left: 250.0, right: 10.0),
child: RaisedButton(
onPressed: () {
setState(() {
msg.add(Controller.text);
Controller.clear();
});
},
child: Text('Add'),
),
),
Expanded(
flex: 2,
child: Container(
child: Card(
margin: EdgeInsets.all(8.0),
child: ListView.builder(
itemCount: msg.length,
itemBuilder: (context, index) {
return new GestureDetector(
onLongPress: () {
if(selectedItems.contains(index))
selectedItems.remove(index);
else
selectedItems.add(index);
},
onTap: () {
if(selectedItems.contains(index))
selectedItems.remove(index);
else
selectedItems.add(index);
},
child: index.isInfinite
? Divider()
: ListTile(
title: Text(msg[index]),
));
}),
),
)),
],
),
);
}
void _deleteItems(){ // call _deleteItems() on clicking delete button
setState(() {
//set your state
for (final index in selectedItems)
msg.removeAt(index);
selectedItems.clear();
});
}
}