movie and tvshow list
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
|
||||
import 'package:syncfusion_flutter_core/theme.dart';
|
||||
|
||||
import 'package:multimedia/data/movies.dart';
|
||||
|
||||
import 'package:multimedia/constants.dart';
|
||||
|
||||
class MoviesScreen extends StatefulWidget {
|
||||
const MoviesScreen({
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _MoviesScreenState();
|
||||
}
|
||||
|
||||
class _MoviesScreenState extends State<MoviesScreen> {
|
||||
final DataGridController _dataGridController = DataGridController();
|
||||
late MovieDataSource movieDataSource;
|
||||
List<GridColumn> _columns = [];
|
||||
|
||||
Future<List<Movie>> generateMovieList() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
String? dbProtocol = prefs.getString('dbProtocol');
|
||||
String? dbHost = prefs.getString('dbHost');
|
||||
String? dbPath = prefs.getString('dbPath');
|
||||
|
||||
Uri url = Uri(
|
||||
scheme: dbProtocol,
|
||||
host: dbHost,
|
||||
path: dbPath,
|
||||
queryParameters: {
|
||||
'module': 'movie',
|
||||
'function': 'getList',
|
||||
},
|
||||
);
|
||||
|
||||
final response = await http.get(url);
|
||||
bool? error;
|
||||
String? errorMessage;
|
||||
|
||||
Map<String, dynamic> movies = json.decode(response.body);
|
||||
error = movies['error'];
|
||||
errorMessage = movies['errmsg'];
|
||||
var list = movies['data'];
|
||||
|
||||
List<Movie> _movies =
|
||||
await list.map<Movie>((json) => Movie.fromJson(json)).toList();
|
||||
movieDataSource = MovieDataSource(_movies);
|
||||
return _movies;
|
||||
}
|
||||
|
||||
List<GridColumn> getColumns() {
|
||||
return <GridColumn>[
|
||||
GridColumn(
|
||||
columnName: 'movieID',
|
||||
visible: false,
|
||||
width: 70,
|
||||
label: Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Text('movieID'))),
|
||||
GridColumn(
|
||||
columnName: 'state',
|
||||
visible: false,
|
||||
width: 70,
|
||||
label: Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Text('state'))),
|
||||
GridColumn(
|
||||
columnName: 'movieTitle',
|
||||
columnWidthMode: ColumnWidthMode.auto,
|
||||
label: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Text('Title'))),
|
||||
GridColumn(
|
||||
columnName: 'releaseDate',
|
||||
columnWidthMode: ColumnWidthMode.auto,
|
||||
label: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Text('First Aired'))),
|
||||
GridColumn(
|
||||
columnName: 'resolution',
|
||||
columnWidthMode: ColumnWidthMode.auto,
|
||||
label: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Text('Resolution'))),
|
||||
GridColumn(
|
||||
columnName: 'overview',
|
||||
columnWidthMode: ColumnWidthMode.auto,
|
||||
label: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: const Text('Overview'))),
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_columns = getColumns();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: FutureBuilder<Object>(
|
||||
future: generateMovieList(),
|
||||
builder: (context, data) {
|
||||
return data.hasData
|
||||
? SfDataGridTheme(
|
||||
data: const SfDataGridThemeData(
|
||||
headerColor: Color(0xff009889),
|
||||
),
|
||||
child: SfDataGrid(
|
||||
source: movieDataSource,
|
||||
selectionMode: SelectionMode.single,
|
||||
navigationMode: GridNavigationMode.row,
|
||||
columns: _columns,
|
||||
controller: _dataGridController,
|
||||
columnWidthMode: ColumnWidthMode.fill,
|
||||
),
|
||||
)
|
||||
: const Center(
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
value: 0.8,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MovieDataSource extends DataGridSource {
|
||||
/// Creates the employee data source class with required details.
|
||||
MovieDataSource(this.movies) {
|
||||
buildDataGridRow();
|
||||
}
|
||||
|
||||
void buildDataGridRow() {
|
||||
_movieDataGridRows = movies
|
||||
.map<DataGridRow>((e) => DataGridRow(cells: [
|
||||
DataGridCell<int>(columnName: 'movieID', value: e.movieID),
|
||||
DataGridCell<int>(columnName: 'state', value: e.state),
|
||||
DataGridCell<String>(
|
||||
columnName: 'movieTitle', value: e.movieTitle),
|
||||
DataGridCell<String>(
|
||||
columnName: 'releaseDate',
|
||||
value: DateFormat('yyyy-MM-dd').format(e.releaseDate)),
|
||||
DataGridCell<String>(
|
||||
columnName: 'resolution', value: e.resolution),
|
||||
DataGridCell<String>(columnName: 'overview', value: e.overview),
|
||||
]))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<Movie> movies = [];
|
||||
|
||||
List<DataGridRow> _movieDataGridRows = [];
|
||||
|
||||
@override
|
||||
List<DataGridRow> get rows => _movieDataGridRows;
|
||||
|
||||
@override
|
||||
DataGridRowAdapter buildRow(DataGridRow row) {
|
||||
return DataGridRowAdapter(
|
||||
cells: row.getCells().map<Widget>((e) {
|
||||
Color colorBG = Colors.white;
|
||||
Color colorFG = Colors.black;
|
||||
|
||||
if (e.columnName == 'movieTitle') {
|
||||
int state = row.getCells().toList()[1].value;
|
||||
|
||||
if (state == 1) {
|
||||
colorBG = StateColor.init;
|
||||
} else if (state == 2) {
|
||||
colorBG = StateColor.prog;
|
||||
colorFG = Colors.white;
|
||||
} else {
|
||||
colorBG = StateColor.done;
|
||||
}
|
||||
}
|
||||
|
||||
return Container(
|
||||
alignment: Alignment.centerLeft,
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
color: colorBG,
|
||||
child: Text(
|
||||
e.value.toString(),
|
||||
style: TextStyle(color: colorFG),
|
||||
),
|
||||
);
|
||||
}).toList());
|
||||
}
|
||||
|
||||
void updateDataGrid() {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user