main.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:permission_handler/permission_handler.dart';
  4. import 'examples/advanced/index.dart';
  5. import 'examples/basic/index.dart';
  6. import 'config/agora.config.dart' as config;
  7. import 'components/log_sink.dart';
  8. void main() => runApp(const MyApp());
  9. /// This widget is the root of your application.
  10. class MyApp extends StatefulWidget {
  11. /// Construct the [MyApp]
  12. const MyApp({Key? key}) : super(key: key);
  13. @override
  14. State<MyApp> createState() => _MyAppState();
  15. }
  16. class _MyAppState extends State<MyApp> {
  17. final _data = [...basic, ...advanced];
  18. bool _showPerformanceOverlay = false;
  19. bool _isConfigInvalid() {
  20. return config.appId == '<YOUR_APP_ID>' ||
  21. config.token == '<YOUR_TOKEN>' ||
  22. config.channelId == '<YOUR_CHANNEL_ID>';
  23. }
  24. @override
  25. void initState() {
  26. super.initState();
  27. _requestPermissionIfNeed();
  28. }
  29. Future<void> _requestPermissionIfNeed() async {
  30. if (defaultTargetPlatform == TargetPlatform.android) {
  31. await [Permission.microphone, Permission.camera].request();
  32. }
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return MaterialApp(
  37. showPerformanceOverlay: _showPerformanceOverlay,
  38. theme: ThemeData(
  39. primarySwatch: Colors.blue,
  40. ),
  41. home: Scaffold(
  42. appBar: AppBar(
  43. title: const Text('APIExample'),
  44. actions: [
  45. ToggleButtons(
  46. color: Colors.grey[300],
  47. selectedColor: Colors.white,
  48. renderBorder: false,
  49. children: const [
  50. Icon(
  51. Icons.data_thresholding_outlined,
  52. )
  53. ],
  54. isSelected: [_showPerformanceOverlay],
  55. onPressed: (index) {
  56. setState(() {
  57. _showPerformanceOverlay = !_showPerformanceOverlay;
  58. });
  59. },
  60. )
  61. ],
  62. ),
  63. body: _isConfigInvalid()
  64. ? const InvalidConfigWidget()
  65. : ListView.builder(
  66. itemCount: _data.length,
  67. itemBuilder: (context, index) {
  68. return _data[index]['widget'] == null
  69. ? Ink(
  70. color: Colors.grey,
  71. child: ListTile(
  72. title: Text(_data[index]['name'] as String,
  73. style: const TextStyle(
  74. fontSize: 24, color: Colors.white)),
  75. ),
  76. )
  77. : ListTile(
  78. onTap: () {
  79. Navigator.push(
  80. context,
  81. MaterialPageRoute(
  82. builder: (context) => Scaffold(
  83. appBar: AppBar(
  84. title: Text(
  85. _data[index]['name'] as String),
  86. // ignore: prefer_const_literals_to_create_immutables
  87. actions: [const LogActionWidget()],
  88. ),
  89. body:
  90. _data[index]['widget'] as Widget?,
  91. )));
  92. },
  93. title: Text(
  94. _data[index]['name'] as String,
  95. style: const TextStyle(
  96. fontSize: 24, color: Colors.black),
  97. ),
  98. );
  99. },
  100. ),
  101. ),
  102. );
  103. }
  104. }
  105. /// This widget is used to indicate the configuration is invalid
  106. class InvalidConfigWidget extends StatelessWidget {
  107. /// Construct the [InvalidConfigWidget]
  108. const InvalidConfigWidget({Key? key}) : super(key: key);
  109. @override
  110. Widget build(BuildContext context) {
  111. return Container(
  112. color: Colors.red,
  113. child: const Text(
  114. 'Make sure you set the correct appId, token, channelId, etc.. in the lib/config/agora.config.dart file.'),
  115. );
  116. }
  117. }