Code as seen in the video:
import 'package:flutter/material.dart';
import 'package:lessons/widget_creator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = TextEditingController();
List<String> _namesList = ["Micky", "Donald", "Goofy", "Pluto"];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
createHeaders("Flutter University hehehe", "Working with Lists"),
vSpacer(10.0),
createTextField(
_controller, "Name", "Enter your name here", Icons.abc),
vSpacer(10.0),
ElevatedButton(
onPressed: () {
setState(() {
_namesList.add(_controller.text);
_namesList.sort();
});
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
child: const Text(
"Add To List",
style: TextStyle(
color: Colors.white,
),
),
),
vSpacer(15.0),
Expanded(
child: createListview(),
),
],
),
),
),
);
}
Widget createListview() {
return ListView.builder(
itemCount: _namesList.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(_namesList[index]),
onTap: () => showDialogBox("Selected Student", _namesList[index]),
),
);
},
);
}
void showDialogBox(String title, String content) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(content),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text(
"Close",
),
),
],
);
});
}
}
No comments:
Post a Comment