CameraMain.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import 'dart:io';
  2. import 'package:camera/camera.dart';
  3. import 'package:camera/new/src/support_android/camera.dart';
  4. import 'package:douyin_demo/providers/CameraProvider.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:provider/provider.dart';
  7. import 'package:path/path.dart' as p;
  8. import 'package:image_picker_saver/image_picker_saver.dart';
  9. class CameraPage extends StatelessWidget {
  10. const CameraPage({Key key}) : super(key: key);
  11. @override
  12. Widget build(BuildContext context) {
  13. return Scaffold(
  14. backgroundColor: Theme.of(context).primaryColor,
  15. body: MultiProvider(providers: [
  16. ChangeNotifierProvider(
  17. builder: (_) => CameraProvider(),
  18. )
  19. ], child: CameraMain()),
  20. bottomNavigationBar: BottomAppBar(),
  21. );
  22. }
  23. }
  24. // class CameraMain extends StatefulWidget {
  25. // CameraMain({Key key}) : super(key: key);
  26. // _CameraMainState createState() => _CameraMainState();
  27. // }
  28. // class _CameraMainState extends State<CameraMain> {
  29. // var cameras;
  30. // CameraController _controller;
  31. // @override
  32. // initState() {
  33. // // TODO: implement initState
  34. // super.initState();
  35. // initCamera();
  36. // }
  37. // Future initCamera() async {
  38. // cameras = await availableCameras();
  39. // _controller = CameraController(cameras[0], ResolutionPreset.medium);
  40. // _controller.initialize().then((_) {
  41. // if (!mounted) {
  42. // return;
  43. // }
  44. // setState(() {});
  45. // });
  46. // }
  47. // @override
  48. // void dispose() {
  49. // // TODO: implement dispose
  50. // _controller?.dispose();
  51. // super.dispose();
  52. // }
  53. // @override
  54. // Widget build(BuildContext context) {
  55. // }
  56. // }
  57. class CameraMain extends StatelessWidget {
  58. const CameraMain({Key key}) : super(key: key);
  59. @override
  60. Widget build(BuildContext context) {
  61. CameraProvider provider = Provider.of<CameraProvider>(context);
  62. if (provider == null || provider.cameraController == null) {
  63. return Container(
  64. child: Center(child: CircularProgressIndicator()),
  65. );
  66. }
  67. double rpx = MediaQuery.of(context).size.width / 750;
  68. double toTop = 100 * rpx;
  69. double outBox = 170 * rpx;
  70. double innerBox = 130 * rpx;
  71. CameraController _controller = provider.cameraController;
  72. var cameras = provider.cameras;
  73. if (_controller == null || _controller?.value == null) {
  74. return Container(
  75. child: Center(child: CircularProgressIndicator()),
  76. );
  77. }
  78. final size = MediaQuery.of(context).size;
  79. return _controller.value.isInitialized
  80. ? Stack(children: <Widget>[
  81. // Camera.open(cameraId),
  82. ClipRect(
  83. child: Transform.scale(
  84. scale: _controller.value.aspectRatio / size.aspectRatio,
  85. child: Center(
  86. child: AspectRatio(
  87. aspectRatio: _controller.value.aspectRatio,
  88. child: CameraPreview(_controller),
  89. ),
  90. ),
  91. )
  92. ),
  93. Positioned(
  94. //顶部关闭按钮
  95. top: toTop,
  96. left: 30 * rpx,
  97. child: IconButton(
  98. icon: Icon(
  99. Icons.close,
  100. color: Colors.white,
  101. size: 60 * rpx,
  102. ),
  103. onPressed: () {
  104. Navigator.pop(context);
  105. },
  106. ),
  107. ),
  108. Positioned(
  109. //选择音乐
  110. top: toTop,
  111. left: 250 * rpx,
  112. child: Container(
  113. width: 250 * rpx,
  114. child: FlatButton(
  115. onPressed: () {},
  116. child: Row(
  117. children: <Widget>[
  118. Icon(
  119. Icons.music_note,
  120. color: Colors.white,
  121. ),
  122. SizedBox(
  123. width: 10 * rpx,
  124. ),
  125. Text(
  126. "选择音乐",
  127. style: TextStyle(color: Colors.white),
  128. ),
  129. ],
  130. ),
  131. ),
  132. ),
  133. ),
  134. Positioned(
  135. //拍照按钮
  136. bottom: 60 * rpx,
  137. // left: (750*rpx-outBox)/2,
  138. child: Container(
  139. width: 750 * rpx,
  140. child: Row(
  141. mainAxisAlignment: MainAxisAlignment.spaceAround,
  142. children: [
  143. IconWithText(
  144. icon: Icon(
  145. Icons.search,
  146. color: Colors.white,
  147. ),
  148. text: "道具"),
  149. CircleTakePhoto(
  150. outBox: outBox,
  151. innerBox: innerBox,
  152. ),
  153. IconWithText(
  154. icon: Icon(
  155. Icons.search,
  156. color: Colors.white,
  157. ),
  158. text: "道具"),
  159. ])),
  160. ),
  161. Positioned(
  162. right: 30 * rpx,
  163. top: 80 * rpx,
  164. child: IconButton(
  165. icon: Icon(Icons.camera_front),
  166. onPressed: () {
  167. provider.changeCamera();
  168. }),
  169. )
  170. ])
  171. : Container();
  172. }
  173. }
  174. class CircleTakePhoto extends StatelessWidget {
  175. const CircleTakePhoto(
  176. {Key key, @required this.outBox, @required this.innerBox})
  177. : super(key: key);
  178. final double outBox;
  179. final double innerBox;
  180. @override
  181. Widget build(BuildContext context) {
  182. double rpx = MediaQuery.of(context).size.width / 750;
  183. CameraProvider provider = Provider.of<CameraProvider>(context);
  184. // double outBox=160*rpx;
  185. // double innerBox=130*rpx;
  186. return Container(
  187. width: outBox,
  188. height: outBox,
  189. padding: EdgeInsets.all(10 * rpx),
  190. decoration: BoxDecoration(
  191. color: Colors.transparent,
  192. borderRadius: BorderRadius.circular(90 * rpx),
  193. border: Border.all(
  194. width: 10 * rpx, color: Color.fromARGB(128, 219, 48, 85)),
  195. ),
  196. child: FlatButton(
  197. padding: EdgeInsets.all(0),
  198. onPressed: () async {
  199. provider.changeFileName();
  200. print(provider.fileName);
  201. await provider.cameraController.takePicture(provider.fileName).then((_){
  202. // Navigator.push(context, MaterialPageRoute(fullscreenDialog: true,builder: (_){
  203. // return Image.file(File(provider.fileName) );
  204. // }));
  205. ImagePickerSaver.saveFile(fileData: File(provider.fileName).readAsBytesSync());
  206. });
  207. },
  208. child: Container(
  209. width: innerBox,
  210. height: innerBox,
  211. alignment: Alignment.center,
  212. decoration: BoxDecoration(
  213. color: Color.fromARGB(255, 219, 48, 85),
  214. borderRadius: BorderRadius.circular(75 * rpx)),
  215. )
  216. ),
  217. );
  218. }
  219. }
  220. class IconWithText extends StatelessWidget {
  221. const IconWithText({Key key, @required this.icon, @required this.text})
  222. : super(key: key);
  223. final Icon icon;
  224. final String text;
  225. @override
  226. Widget build(BuildContext context) {
  227. return Column(
  228. children: <Widget>[
  229. icon,
  230. Text(
  231. text,
  232. style: TextStyle(color: Colors.white),
  233. )
  234. ],
  235. );
  236. }
  237. }