Send Listview Selected Item To Another Activity Screen In Flutter

admin_img Posted By Bajarangi soft , Posted On 12-10-2020

In flutter we can easily access the ListView selected item value but the main task is to Flutter Send ListView Selected Item to Another Activity Screen and receive the sent value on next screen using super(key: key) method. This method would allow us to receive values sent by another activity screen on navigation time.

Send Listview Selected Item To Another Activity Screen In Flutter

Send Listview Selected Item To Another Activity Screen
Complete Code For Send Listview Selected Item To Another Activity Screen In Flutter 
main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: HomeScreen(),
  ));
}

class HomeScreen extends StatelessWidget{
  final List<String> Fruitsitems = [
    'Apple',
    'Bannana',
    'Mango',
    'Orange',
    'Watermelan',
    'Pinepal',
    'Chikku',
    'Gauvava',
  ];

  getItemAndNavigate(String item, BuildContext context){
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => SecondScreen(FruitsItemHolder : item)
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Home Activity Screen"),
        ),
        body: Center(
          child: ListView(
            children: Fruitsitems
                .map((data) => ListTile(
                title: Text(data),
                onTap: ()=>{
                  getItemAndNavigate(data, context)
                }
            ))
                .toList(),
          ),
        )
    );
  }
}

class SecondScreen extends StatelessWidget {
  final String FruitsItemHolder ;
  SecondScreen({Key key, @required this.FruitsItemHolder}) : super(key: key);
  goBack(BuildContext context){
    Navigator.pop(context);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFFCDDC39),
          title: Text("Second Activity Screen"),
        ),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Center(child:
              Text('Fruit Name = ' + FruitsItemHolder,
                style: TextStyle(fontSize: 22),
                textAlign: TextAlign.center,)),

              RaisedButton(
                onPressed: () {goBack(context);},
                color: Color(0xFFCDDC39),
                textColor: Colors.white,
                child: Text('Go Back To Previous Screen'),
              )])
    );
  }
}

Related Post